package com.yanqu.road.server.manger.activity.chinesechess;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chinesechess.*;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.chinesechess.eChineseChessGameStatusType;
import com.yanqu.road.entity.enums.activity.chinesechess.eChineseChessPiecesType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.activity.ChineseChessBusiness;
import com.yanqu.road.pb.activity.ChineseChessProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.chess.ChineseChessModule;
import com.yanqu.road.server.gameplayer.module.activity.chess.ChineseChessPracticeModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ChineseChessMgr extends TempMgr {

    private static RandomHelper randomHelper = new RandomHelper();

    private static ActivityInfo activityInfo;
    private static Map<Integer,Boolean> isSendRewardMap = new ConcurrentHashMap<>();


    private static Map<Integer, ChineseChessConfig> configChessMap = new ConcurrentHashMap<>();
    private static Map<Integer, ChineseChessCreateConfig> configChessCreateMap = new ConcurrentHashMap<>();
    private static Map<Integer, ChineseChessWeightConfig> configChessWeightMap = new ConcurrentHashMap<>();
    private static Map<Integer, ChineseChessMoveConfig> configChessMoveMap = new ConcurrentHashMap<>();

    private static boolean debug = false;

    //每日零点重置多少次数
    private static int recoverHp;

    /*不同模式对应的配置：普通场；*/

    //复活次数对应消耗元宝
    private static List<Integer> reviveList = new ArrayList<>();

    //玩家初始位置
    private static ChineseChessPosition initialSelfPosition;//自己棋子的初始位置
    private static List<ChineseChessData> initialEnemyChessList;//敌人棋子的初始位置

    private static List<Integer> scoreShuaiList;//生成帅的分数值
    private static List<ChineseChessData> shuaiAttendList;//帅的保护棋子

    //盘面长度
    private static int x = 9;
    //盘面宽度
    private static int y = 10;

    //所有棋谱
    private static Map<Integer,Map<Integer,ChineseChessPosition>> allPositionMap = new ConcurrentHashMap<>();
    private static Map<String,ChineseChessPosition> allPositionSimpleMap = new ConcurrentHashMap<>();

    private static int CHINESE_CHESS_SCORE_ITEM_ID = 101998;//积分道具

    private static int CHINESE_CHESS_PLAY_ITEM_ID = 120725;//对局卡

    private static int CHINESE_CHESS_POWER_ITEM_ID = 120727;//强化卡

    private static int CHINESE_CHESS_REVIVE_ITEM_ID = 120726;//分身道具

    private static String BIGCHESS_BOARD_XY = "9*10";//棋盘大小

    private static int BIGCHESS_FREE_PLAYNUM = 3;//玩家每天免费入场次数

    private static String BIGCHESS_MYCHESS_SKIN_ADD = "123456;100|120737;200";//我的棋子皮肤加成（千分比）

    private static String BIGCHESS_EVALUATE_SCORE = "1001;0|1002;100|1003;200|1004;300|1005;500";//评价文本及达成分数

    private static String BIGCHESS_PLAYER_BEGIN_PLACE = "4;2";//玩家初始位置（X;Y）
    private static String BIGCHESS_ENEMY_BEGIN_PLACE = "1;6;1001|3;6;1001|5;6;1001";//敌人初始位置（X;Y）

    private static String BIGCHESS_REBORN_COST = "100|200";//复活相关配置消耗元宝（第一次复活元宝消耗 | 第二次……）

    private static int BIGCHESS_NEW_PROTECT_REGION = 10;//新手保护：危险区域提示  步数线
    private static int BIGCHESS_NEW_PROTECT_REBORN = 5;//新手保护：死亡复活  步数线
    private static int BIGCHESS_REBORN_ISOPEN = 1;//是否开启元宝复活功能
    private static String BIGCHESS_EVALUATE_TIPS = "100;20|200;50|300;70|400;90|500;99";//超越分数文本提示（达成分数；超越百分比）
    private static int BIGCHESS_AICHESS_MAX_NUM_CHE = 2;//敌方最大棋子数量：车
    private static int BIGCHESS_AICHESS_MAX_NUM_MA = 5;//敌方最大棋子数量：马
    private static String BIGCHESS_AICHESS_BORNLIMIT_JIANG = "15;30;50;60";//将的生成分数限制:第一次生成间隔分数；第二次生成间隔分数……，次数超过配置次数取最后的配置数
    private static String BIGCHESS_AICHESS_WITHJIANG_NUM = "1002;2";//将的生成分数限制:第一次生成间隔分数；第二次生成间隔分数……，次数超过配置次数取最后的配置数
    private static int BIGCHESS_SKILL_WAVE_NUM = 3;//吃了帅之后的强化次数


    private static int BIGCHESS_TRAIN_ISOPEN = 1;//是不是有练习模式
    private static int BIGCHESS_ROUND_TURN_SCORE = 10;//通关后额外步数加分数


    private static List<Integer> tParamList;
    private static List<Integer> oParamList;
    private static List<Integer> oValueList;
    private static List<Integer> paramList;

    private static String BIGCHESS_CREATCHESS_T_PARAM = "1000|2|6";//通路参数t、σ、r
    private static String BIGCHESS_CREATCHESS_O_PARAM = "1000|1|6";//耦合参数t、σ、r
    private static String BIGCHESS_CREATCHESS_O_VALUE = "1|3|5|10";//耦合位置的数值参数

    private static String BIGCHESS_CREATCHESS_PARAM = "1000|15|1800|10";//耦合位置的数值参数

    private static int BIGCHESS_WIN_ROUND = 170;//直接通关了
    private static String BIGCHESS_WIN_REWARD = "110062=1";
    private static String BIGCHESS_EVALUATE_REWARD = "110062=1|110062=2|110062=3|110062=4|110062=5";//耦合位置的数值参数
    private static Map<Integer, String> gameOverRewardMap;




    private static List<Long> debugUserIdList = new ArrayList<>();
    private static String DEBUG_USERID_LIST = "888810000000001";//哪些用户会加上下棋，方便测试

    public static boolean isDebug(){
        return debug;
    }

    public static boolean isDebugUserId(long userId){
        return debugUserIdList.contains(userId);
    }

    public static ChineseChessProto.ChineseChessConfigDataTemp.Builder getConfigDataTempBuilder(String language) {
        ChineseChessProto.ChineseChessConfigDataTemp.Builder msgBuilder = ChineseChessProto.ChineseChessConfigDataTemp.newBuilder();
        for(ChineseChessConfig config : configChessMap.values()){
            if(config.getId() == 0)continue;
            ChineseChessProto.ChessConfigTemp.Builder tempBuilder = ChineseChessProto.ChessConfigTemp.newBuilder();
            tempBuilder.setId(config.getId());
            tempBuilder.setScore(config.getScore());
            tempBuilder.setChessSkinId(config.getChessSkinId());
            tempBuilder.setCanGo(config.getCanGo());
            tempBuilder.setMoveLimit(config.getMoveLimit());
            tempBuilder.setMoveText(ServerLanguageMgr.getContent(config.getMoveText(),
                    language));
            tempBuilder.setEatText(ServerLanguageMgr.getContent(config.getEatText(),
                    language));
            msgBuilder.addChess(tempBuilder);
        }
        return msgBuilder;
    }

    public static int getPassStepAdd(){
        return BIGCHESS_ROUND_TURN_SCORE;
    }

    public static int getEatKingPowerTime(){
        return BIGCHESS_SKILL_WAVE_NUM;
    }

    public static int getScoreItemId(){
        return CHINESE_CHESS_SCORE_ITEM_ID;
    }

    public static int getPowerItemId(){
        return CHINESE_CHESS_POWER_ITEM_ID;
    }
    public static int getChineseChessReviveItemId(){
        return CHINESE_CHESS_REVIVE_ITEM_ID;
    }

    public static int getPlayItemId(){
        return CHINESE_CHESS_PLAY_ITEM_ID;
    }

    public static int getEveryHp(){
        return recoverHp;
    }

    public static boolean isIngotRevive(int times){
        return BIGCHESS_REBORN_ISOPEN == 1 && times < reviveList.size();
    }

    public static long getIngotReviveCost(int times){
        if(times < reviveList.size()){
            return reviveList.get(times);
        }
        return Long.MAX_VALUE;
    }

    public static Map<Integer,ChineseChessSkinData> getInitSkinData(){
        Map<Integer,ChineseChessSkinData> initData = new ConcurrentHashMap<>();
        //玩家棋子
        ChineseChessSkinData playerData = new ChineseChessSkinData();
        playerData.setChessId(0);
        playerData.setUseSkinId(0);
        initData.put(0,playerData);
        for(eChineseChessPiecesType chessType : eChineseChessPiecesType.values()){
            ChineseChessSkinData data = new ChineseChessSkinData();
            data.setChessId(chessType.getValue());
            data.setUseSkinId(0);
            initData.put(chessType.getValue(),data);
        }
        return initData;
    }

    private static Map<Integer,Map<Integer,ChineseChessPosition>> addPostMap(Map<Integer,Map<Integer,ChineseChessPosition>> posMap,ChineseChessPosition pos){
        if(!posMap.containsKey(pos.getX())){
            posMap.put(pos.getX(),new ConcurrentHashMap<>());
        }
        posMap.get(pos.getX()).put(pos.getY(),pos);
        return posMap;
    }

    private static Map<Integer,Map<Integer,ChineseChessPosition>> getSelfCanGoMap(ChineseChessData selfChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        Map<Integer,Map<Integer,ChineseChessPosition>> posMap = new ConcurrentHashMap<>();
        for(int i = selfChess.getPosition().getX() + 1;i < x;i++){
            ChineseChessPosition pos = new ChineseChessPosition(i,selfChess.getPosition().getY());
            addPostMap(posMap,pos);
            if(posChessMap.containsKey(pos.getX()) && posChessMap.get(pos.getX()).containsKey(pos.getY())){
                break;
            }
        }
        for(int i = selfChess.getPosition().getX() - 1;i >= 0;i--){
            ChineseChessPosition pos = new ChineseChessPosition(i,selfChess.getPosition().getY());
            addPostMap(posMap,pos);
            if(posChessMap.containsKey(pos.getX()) && posChessMap.get(pos.getX()).containsKey(pos.getY())){
                break;
            }
        }
        for(int i = selfChess.getPosition().getY() + 1;i < y;i++){
            ChineseChessPosition pos = new ChineseChessPosition(selfChess.getPosition().getX(),i);
            addPostMap(posMap,pos);
            if(posChessMap.containsKey(pos.getX()) && posChessMap.get(pos.getX()).containsKey(pos.getY())){
                break;
            }
        }
        for(int i = selfChess.getPosition().getY() - 1;i >= 0;i--){
            ChineseChessPosition pos = new ChineseChessPosition(selfChess.getPosition().getX(),i);
            addPostMap(posMap,pos);
            if(posChessMap.containsKey(pos.getX()) && posChessMap.get(pos.getX()).containsKey(pos.getY())){
                break;
            }
        }
        return posMap;
    }

    private static Map<Integer,Map<Integer,ChineseChessPosition>> getSelfCanGoNoEatMap(ChineseChessData selfChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        Map<Integer,Map<Integer,ChineseChessPosition>> posMap = new ConcurrentHashMap<>();
        for(int i = selfChess.getPosition().getX() + 1;i < x;i++){
            int moveX = i;
            int moveY = selfChess.getPosition().getY();
            if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                break;
            }
            addPostMap(posMap,new ChineseChessPosition(moveX,moveY));
        }
        for(int i = selfChess.getPosition().getX() - 1;i >= 0;i--){
            int moveX = i;
            int moveY = selfChess.getPosition().getY();
            if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                break;
            }
            addPostMap(posMap,new ChineseChessPosition(moveX,moveY));
        }
        for(int i = selfChess.getPosition().getY() + 1;i < y;i++){
            int moveX = selfChess.getPosition().getX();
            int moveY = i;
            if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                break;
            }
            addPostMap(posMap,new ChineseChessPosition(moveX,moveY));
        }
        for(int i = selfChess.getPosition().getY() - 1;i >= 0;i--){
            int moveX = selfChess.getPosition().getX();
            int moveY = i;
            if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                break;
            }
            addPostMap(posMap,new ChineseChessPosition(moveX,moveY));
        }
        return posMap;
    }

    //开启强化后可以移动的位置
    private static Map<Integer,Map<Integer,ChineseChessPosition>> getPlayerCanGoPowerMap(ChineseChessData playerChess){
        int playerX = playerChess.getPosition().getX();
        int playerY = playerChess.getPosition().getY();
        Map<Integer,Map<Integer,ChineseChessPosition>> posMap = new ConcurrentHashMap<>();
        for(int i = 0;i < x;i++){
            if(i == playerX)continue;
            addPostMap(posMap,new ChineseChessPosition(i,playerY));
        }
        for(int i = 0;i < y;i++){
            if(i == playerY)continue;
            addPostMap(posMap,new ChineseChessPosition(playerX,i));
        }
        return posMap;
    }

    public static long getNewPlayerStep(){
        return BIGCHESS_NEW_PROTECT_REBORN;
    }

    private static void addPosition(List<ChineseChessPosition> posList,int x,int y){
        ChineseChessPosition pos = new ChineseChessPosition();
        pos.setX(x);
        pos.setY(y);
        posList.add(pos);
    }

    //进入可抓列表的棋子 只有马和士和象被吃可抓,其他棋子出现被吃去抓就很傻逼
    private static boolean isMoveEatChess(int chessId){
        if(chessId == eChineseChessPiecesType.Mandarins.getValue()){
            return true;
        }
        if(chessId == eChineseChessPiecesType.Elephants.getValue()){
            return true;
        }
        if(chessId == eChineseChessPiecesType.Knights.getValue()){
            return true;
        }
        return false;
    }

    //要校验被吃跑路会增加玩家移动范围的棋子，支线跑路的棋子，出现自己跑了但是没跑的情况，要横着走
    private static boolean isMoveNoMeanChess(int chessId){
        if(chessId == eChineseChessPiecesType.Pawns.getValue()){
            return true;
        }
        if(chessId == eChineseChessPiecesType.Cannons.getValue()){
            return true;
        }
        if(chessId == eChineseChessPiecesType.Rooks.getValue()){
            return true;
        }
        if(chessId == eChineseChessPiecesType.King.getValue()){
            return true;
        }
        return false;
    }

    /**
     * AI棋子移动后的位置是不是下一回合可以吃玩家,只支持配置了移动的，车和炮不计算
     * @param ochess
     * @param moveX
     * @param moveY
     * @param posChessMap
     * @return
     */
    private static boolean isMoveEatPlayer(ChineseChessData ochess,int moveX,int moveY,Map<Integer,Map<Integer,ChineseChessData>> posChessMap){
        if(!isMoveEatChess(ochess.getChessId())){
            return false;
        }
        ChineseChessConfig chessConfig = getChessConfig(ochess.getChessId());
        Map<Integer,ChineseChessPosition> movePositionMap =  chessConfig.getCanGoMap();
        Map<Integer,ChineseChessPosition> moveLimitMap =  chessConfig.getMoveLimitMap();
        ChineseChessData moveChess = new ChineseChessData();
        moveChess.setChessId(ochess.getChessId());
        moveChess.setPosition(new ChineseChessPosition(moveX,moveY));
        for(int moveIndex : movePositionMap.keySet()){
            ChineseChessPosition pos = movePositionMap.get(moveIndex);
            ChineseChessPosition posLimit = moveLimitMap.get(moveIndex);
            if(canMoveNoChess(moveChess,pos)){
                if(posLimit != null){
                    int limitMoveX = moveChess.getPosition().getX() + posLimit.getX();
                    int limitMoveY = moveChess.getPosition().getY() + posLimit.getY();
                    //被绊住不能走了
                    if(!(posChessMap.containsKey(limitMoveX) && posChessMap.get(limitMoveX).containsKey(limitMoveY))){
                        continue;
                    }
                }
                int move2X = moveChess.getPosition().getX() + pos.getX();
                int move2Y = moveChess.getPosition().getY() + pos.getY();
                if(posChessMap.containsKey(move2X) && posChessMap.get(move2X).containsKey(move2Y)){
                    if(posChessMap.get(move2X).get(move2Y).getChessNo() == 0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private static int randomOneMoveChess(Map<Integer,List<ChineseChessPosition>> oMap){
        if(oMap.size() ==  0){
            return -1;
        }
        List<Integer> keyMap = new ArrayList<>(oMap.keySet());
        int randomNum = randomHelper.next(0, keyMap.size());
        return keyMap.get(randomNum);
    }

    private static ChineseChessPosition randomOneChessMove(List<ChineseChessPosition> moveList){
        if(moveList.size() == 0){
            return null;
        }
        int randomNum = randomHelper.next(0, moveList.size());
        return moveList.get(randomNum);
    }

    private static int randomOneMoveChessMerge(Map<Integer,List<ChineseChessPosition>> oMap,Map<Integer,List<ChineseChessPosition>> tMap){
        List<Integer> chessNoList = new ArrayList<>();
        for(Integer chessNo : oMap.keySet()){
            if(tMap.containsKey(chessNo)){
                chessNoList.add(chessNo);
            }
        }
        if(chessNoList.size() == 0){
            return -1;
        }
        int randomNum = randomHelper.next(0, chessNoList.size());
        return chessNoList.get(randomNum);
    }

    private static Map<Integer,Map<Integer,ChineseChessData>> getChessPosKeyMap(Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = new ConcurrentHashMap<>();
        for(ChineseChessData chess : chessMap.values()){
            if(!posChessMap.containsKey(chess.getPosition().getX())){
                posChessMap.put(chess.getPosition().getX(),new ConcurrentHashMap<>());
            }
            posChessMap.get(chess.getPosition().getX()).put(chess.getPosition().getY(),chess);
        }
        return posChessMap;
    }

    private static Map<Integer,Map<Integer,ChineseChessPosition>> addPosKeyMap(Map<Integer,Map<Integer,ChineseChessPosition>> posMap,List<ChineseChessPosition> posList){
        for(ChineseChessPosition pos : posList){
            if(!posMap.containsKey(pos.getX())){
                posMap.put(pos.getX(),new ConcurrentHashMap<>());
            }
            posMap.get(pos.getX()).put(pos.getY(),pos);
        }
        return posMap;
    }
    /**
     * 获得棋子的可移动位置
     * @param
     * @return 移动的那个棋子
     */
    public static ChineseChessData getNextMove(Map<Integer,ChineseChessData> chessMap,boolean isNoEatMove){
        Map<Integer,List<ChineseChessPosition>> movePosMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> eatPosMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> moveEatPosMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> beEatPosCanMoveMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> beEatPosCanMoveNoEatMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> moveBeEatPosMap = new ConcurrentHashMap<>();
        Map<Integer,List<ChineseChessPosition>> moveNoBeEatPosMap = new ConcurrentHashMap<>();

        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        ChineseChessData playerChess = chessMap.get(0);
        Map<Integer,Map<Integer,ChineseChessPosition>> playerCanMoveMap = getSelfCanGoMap(chessMap.get(0),chessMap);
        for(ChineseChessData chess : chessMap.values()){
            if(chess.getChessNo() == 0)continue;
            List<ChineseChessPosition> movePosList = new ArrayList<>();//可移动列表：此列表的AI棋子有能够移动的点位
            List<ChineseChessPosition> eatPosList = new ArrayList<>();//可吃列表：此列表的AI棋子能直接吃掉玩家棋子
            List<ChineseChessPosition> moveEatPosList = new ArrayList<>();//可抓列表：此列表的AI棋子再走一步能进入可吃列表
            List<ChineseChessPosition> beEatPosCanMoveList = new ArrayList<>();//被吃列表：此列表的AI棋子会被玩家直接吃掉，且是可以移动的
            List<ChineseChessPosition> beEatPosCanMoveNoEatList = new ArrayList<>();//可逃列表：此列表的AI棋子在被吃列表中，走一步后可以脱离被吃列表
            List<ChineseChessPosition> moveBeEatPosList = new ArrayList<>();//走一步被吃列表
            List<ChineseChessPosition> moveNoBeEatPosList = new ArrayList<>();//可移动不被吃列表：此列表的AI棋子有能够移动的点位,且移动不会被吃


            int chessId = chess.getChessId();
            if(chessId == eChineseChessPiecesType.Rooks.getValue()){
                for(int i = chess.getPosition().getX() + 1;i < x;i++){
                    //挡住了，走不了了
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                        //是自己的棋子，直接吃掉
                        if(posChessMap.get(i).get(chess.getPosition().getY()).getChessNo() == 0){
                            addPosition(movePosList,i,chess.getPosition().getY());
                            addPosition(eatPosList,i,chess.getPosition().getY());
                        }
                        break;
                    }
                    addPosition(movePosList,i,chess.getPosition().getY());
                    if(playerCanMoveMap.containsKey(i) && playerCanMoveMap.get(i).containsKey(chess.getPosition().getY())){
                        addPosition(moveBeEatPosList,i,chess.getPosition().getY());
                    }else{
                        addPosition(moveNoBeEatPosList,i,chess.getPosition().getY());
                    }
                }
                for(int i = chess.getPosition().getX() - 1;i >= 0;i--){
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                        if(posChessMap.get(i).get(chess.getPosition().getY()).getChessNo() == 0){
                            addPosition(movePosList,i,chess.getPosition().getY());
                            addPosition(eatPosList,i,chess.getPosition().getY());
                        }
                        break;
                    }
                    addPosition(movePosList,i,chess.getPosition().getY());
                    if(playerCanMoveMap.containsKey(i) && playerCanMoveMap.get(i).containsKey(chess.getPosition().getY())){
                        addPosition(moveBeEatPosList,i,chess.getPosition().getY());
                    }else{
                        addPosition(moveNoBeEatPosList,i,chess.getPosition().getY());
                    }
                }
                for(int i = chess.getPosition().getY() + 1;i < y;i++){
                    if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                        if(posChessMap.get(chess.getPosition().getX()).get(i).getChessNo() == 0){
                            addPosition(movePosList,chess.getPosition().getX(),i);
                            addPosition(eatPosList,chess.getPosition().getX(),i);
                        }
                        break;
                    }
                    addPosition(movePosList,chess.getPosition().getX(),i);
                    if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(i)){
                        addPosition(moveBeEatPosList,chess.getPosition().getX(),i);
                    }else{
                        addPosition(moveNoBeEatPosList,chess.getPosition().getX(),i);
                    }
                }
                for(int i = chess.getPosition().getY() - 1;i >= 0;i--){
                    if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                        if(posChessMap.get(chess.getPosition().getX()).get(i).getChessNo() == 0){
                            addPosition(movePosList,chess.getPosition().getX(),i);
                            addPosition(eatPosList,chess.getPosition().getX(),i);
                        }
                        break;
                    }
                    addPosition(movePosList,chess.getPosition().getX(),i);
                    if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(i)){
                        addPosition(moveBeEatPosList,chess.getPosition().getX(),i);
                    }else{
                        addPosition(moveNoBeEatPosList,chess.getPosition().getX(),i);
                    }
                }
                //当前位置会被玩家吃
                if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(chess.getPosition().getY())){
                    beEatPosCanMoveList = new ArrayList<>(movePosList);
                    for(ChineseChessPosition pos : beEatPosCanMoveList){
                        if(!(playerCanMoveMap.containsKey(pos.getX()) && playerCanMoveMap.get(pos.getX()).containsKey(pos.getY())) &&
                                (pos.getX() != playerChess.getPosition().getX() && pos.getY() != playerChess.getPosition().getY())){
                            //当前会被吃，走哪里会不被吃
                            addPosition(beEatPosCanMoveNoEatList,pos.getX(),pos.getY());
                        }
                    }
                }
            }
            else if(chessId == eChineseChessPiecesType.Cannons.getValue()){
                //遇到敌人炮可以打到后面那个
                boolean meetEnemy = false;
                for(int i = chess.getPosition().getX() + 1;i < x;i++){
                    if(!meetEnemy){
                        if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                            meetEnemy = true;
                        }else{
                            addPosition(movePosList,i,chess.getPosition().getY());
                            if(playerCanMoveMap.containsKey(i) && playerCanMoveMap.get(i).containsKey(chess.getPosition().getY())){
                                addPosition(moveBeEatPosList,i,chess.getPosition().getY());
                            }else{
                                addPosition(moveNoBeEatPosList,i,chess.getPosition().getY());
                            }
                        }
                    }else{
                        //炮打过去
                        if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                            if(posChessMap.get(i).get(chess.getPosition().getY()).getChessNo() == 0){
                                addPosition(movePosList,i,chess.getPosition().getY());
                                addPosition(eatPosList,i,chess.getPosition().getY());
                            }
                            break;
                        }
                    }
                }
                meetEnemy = false;
                for(int i = chess.getPosition().getX() - 1;i >= 0;i--){
                    if(!meetEnemy){
                        if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                            meetEnemy = true;
                        }else{
                            addPosition(movePosList,i,chess.getPosition().getY());
                            if(playerCanMoveMap.containsKey(i) && playerCanMoveMap.get(i).containsKey(chess.getPosition().getY())){
                                addPosition(moveBeEatPosList,i,chess.getPosition().getY());
                            }else{
                                addPosition(moveNoBeEatPosList,i,chess.getPosition().getY());
                            }
                        }
                    }else{
                        if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(chess.getPosition().getY())){
                            if(posChessMap.get(i).get(chess.getPosition().getY()).getChessNo() == 0){
                                addPosition(movePosList,i,chess.getPosition().getY());
                                addPosition(eatPosList,i,chess.getPosition().getY());
                            }
                            break;
                        }
                    }
                }
                meetEnemy = false;
                for(int i = chess.getPosition().getY() + 1;i < y;i++){
                    if(!meetEnemy){
                        if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                            meetEnemy = true;
                        }else{
                            addPosition(movePosList,chess.getPosition().getX(),i);
                            if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(i)){
                                addPosition(moveBeEatPosList,chess.getPosition().getX(),i);
                            }else{
                                addPosition(moveNoBeEatPosList,chess.getPosition().getX(),i);
                            }
                        }
                    }else{
                        if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                            if(posChessMap.get(chess.getPosition().getX()).get(i).getChessNo() == 0){
                                addPosition(movePosList,chess.getPosition().getX(),i);
                                addPosition(eatPosList,chess.getPosition().getX(),i);
                            }
                            break;
                        }
                    }
                }
                meetEnemy = false;
                for(int i = chess.getPosition().getY() - 1;i >= 0;i--){
                    if(!meetEnemy){
                        if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                            meetEnemy = true;
                        }else{
                            addPosition(movePosList,chess.getPosition().getX(),i);
                            if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(i)){
                                addPosition(moveBeEatPosList,chess.getPosition().getX(),i);
                            }else{
                                addPosition(moveNoBeEatPosList,chess.getPosition().getX(),i);
                            }
                        }
                    }else{
                        if(posChessMap.containsKey(chess.getPosition().getX()) && posChessMap.get(chess.getPosition().getX()).containsKey(i)){
                            if(posChessMap.get(chess.getPosition().getX()).get(i).getChessNo() == 0){
                                addPosition(movePosList,chess.getPosition().getX(),i);
                                addPosition(eatPosList,chess.getPosition().getX(),i);
                            }
                            break;
                        }
                    }
                }

                //当前位置会被玩家吃
                if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(chess.getPosition().getY())){
                    beEatPosCanMoveList = new ArrayList<>(movePosList);
                    for(ChineseChessPosition pos : beEatPosCanMoveList){
                        if(!(playerCanMoveMap.containsKey(pos.getX()) && playerCanMoveMap.get(pos.getX()).containsKey(pos.getY())) &&
                                (pos.getX() != playerChess.getPosition().getX() && pos.getY() != playerChess.getPosition().getY())){
                            //当前会被吃，走哪里会不被吃
                            addPosition(beEatPosCanMoveNoEatList,pos.getX(),pos.getY());
                        }
                    }
                }
            }
            else{
                ChineseChessConfig chessConfig = getChessConfig(chess.getChessId());
                Map<Integer,ChineseChessPosition> movePositionMap =  chessConfig.getCanGoMap();
                Map<Integer,ChineseChessPosition> moveLimitMap =  chessConfig.getMoveLimitMap();
                for(int moveIndex : movePositionMap.keySet()){
                    ChineseChessPosition pos = movePositionMap.get(moveIndex);
                    ChineseChessPosition posLimit = moveLimitMap.get(moveIndex);
                    if(canMoveNoChess(chess,pos)){
                        if(posLimit != null){
                            int limitMoveX = chess.getPosition().getX() + posLimit.getX();
                            int limitMoveY = chess.getPosition().getY() + posLimit.getY();
                            //被绊住不能走了
                            if(posChessMap.containsKey(limitMoveX) && posChessMap.get(limitMoveX).containsKey(limitMoveY)){
                                continue;
                            }
                        }
                        int moveX = chess.getPosition().getX() + pos.getX();
                        int moveY = chess.getPosition().getY() + pos.getY();
                        if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                            if(posChessMap.get(moveX).get(moveY).getChessNo() == 0){
                                addPosition(movePosList,moveX,moveY);
                                addPosition(eatPosList,moveX,moveY);
                            }
                        }else{
                            addPosition(movePosList,moveX,moveY);
                            if(playerCanMoveMap.containsKey(moveX) && playerCanMoveMap.get(moveX).containsKey(moveY)){
                                addPosition(moveBeEatPosList,moveX,moveY);
                            }else{
                                addPosition(moveNoBeEatPosList,moveX,moveY);
                            }
                            if(isMoveEatPlayer(chess,moveX,moveY,posChessMap)){
                                addPosition(moveEatPosList,moveX,moveY);
                            }
                        }
                    }
                }
                //当前位置会被玩家吃
                if(playerCanMoveMap.containsKey(chess.getPosition().getX()) && playerCanMoveMap.get(chess.getPosition().getX()).containsKey(chess.getPosition().getY())){
                    beEatPosCanMoveList = new ArrayList<>(movePosList);
                    for(ChineseChessPosition pos : beEatPosCanMoveList){
                        if(!(playerCanMoveMap.containsKey(pos.getX()) && playerCanMoveMap.get(pos.getX()).containsKey(pos.getY()))){
                            //卒和帅移动后可能会出现本来不能移动的位置玩家可以移动，加上特殊判断,不然会出现移动和玩家在一条直线上
                            if(isMoveNoMeanChess(chess.getChessId())){
                                if(pos.getX() != playerChess.getPosition().getX() && pos.getY() != playerChess.getPosition().getY()){
                                    addPosition(beEatPosCanMoveNoEatList,pos.getX(),pos.getY());
                                }
                            }else{
                                //当前会被吃，走哪里会不被吃
                                addPosition(beEatPosCanMoveNoEatList,pos.getX(),pos.getY());
                            }
                        }
                    }
                }
            }
            if(movePosList.size() > 0){
                movePosMap.put(chess.getChessNo(),movePosList);
            }
            if(eatPosList.size() > 0){
                eatPosMap.put(chess.getChessNo(),eatPosList);
            }
            if(beEatPosCanMoveList.size() > 0){
                beEatPosCanMoveMap.put(chess.getChessNo(),beEatPosCanMoveList);
            }
            if(beEatPosCanMoveNoEatList.size() > 0){
                beEatPosCanMoveNoEatMap.put(chess.getChessNo(),beEatPosCanMoveNoEatList);
            }

            if(moveEatPosList.size() > 0){
                moveEatPosMap.put(chess.getChessNo(),moveEatPosList);
            }

            if(moveBeEatPosList.size() > 0){
                moveBeEatPosMap.put(chess.getChessNo(),moveBeEatPosList);
            }
            if(moveNoBeEatPosList.size() > 0){
                moveNoBeEatPosMap.put(chess.getChessNo(),moveNoBeEatPosList);
            }
        }

        if(isNoEatMove){
            removeAllEatPlayerPosition(movePosMap,eatPosMap);
            removeAllEatPlayerPosition(moveEatPosMap,eatPosMap);
            removeAllEatPlayerPosition(beEatPosCanMoveMap,eatPosMap);
            removeAllEatPlayerPosition(beEatPosCanMoveNoEatMap,eatPosMap);
            removeAllEatPlayerPosition(moveBeEatPosMap,eatPosMap);
            removeAllEatPlayerPosition(moveNoBeEatPosMap,eatPosMap);
        }

        //移动逻辑
        int chessNo = -1;
        if(movePosMap.size() > 0){
            if(eatPosMap.size() > 0 && !isNoEatMove){
                chessNo = randomOneMoveChess(eatPosMap);
                //需要回滚回之前的步骤
                chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                chessMap.get(chessNo).setPosition(eatPosMap.get(chessNo).get(0));
                return chessMap.get(chessNo);
            }else{
                if(beEatPosCanMoveMap.size() > 0){
                    chessNo = randomOneMoveChessMerge(beEatPosCanMoveMap,moveEatPosMap);
                    if(moveEatPosMap.containsKey(chessNo)){
                        chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                        chessMap.get(chessNo).setPosition(randomOneChessMove(moveEatPosMap.get(chessNo)));
                        return chessMap.get(chessNo);
                    }
                    if(beEatPosCanMoveNoEatMap.size() > 0){
                        chessNo = randomOneMoveChess(beEatPosCanMoveNoEatMap);
                        chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                        chessMap.get(chessNo).setPosition(randomOneChessMove(beEatPosCanMoveNoEatMap.get(chessNo)));
                        return chessMap.get(chessNo);
                    }
                    chessNo = randomOneMoveChess(beEatPosCanMoveMap);
                    chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                    chessMap.get(chessNo).setPosition(randomOneChessMove(beEatPosCanMoveMap.get(chessNo)));
                    return chessMap.get(chessNo);
                }else{
                    if(moveEatPosMap.size() > 0){
                        chessNo = randomOneMoveChess(moveEatPosMap);
                        chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                        chessMap.get(chessNo).setPosition(randomOneChessMove(moveEatPosMap.get(chessNo)));
                        return chessMap.get(chessNo);
                    }
                    if(moveNoBeEatPosMap.size() > 0){
                        chessNo = randomOneMoveChess(moveNoBeEatPosMap);
                        chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                        chessMap.get(chessNo).setPosition(randomOneChessMove(moveNoBeEatPosMap.get(chessNo)));
                        return chessMap.get(chessNo);
                    }
                    chessNo = randomOneMoveChess(movePosMap);
                    chessMap.get(chessNo).setLastPosition(chessMap.get(chessNo).getPosition());
                    chessMap.get(chessNo).setPosition(randomOneChessMove(movePosMap.get(chessNo)));
                    return chessMap.get(chessNo);
                }
            }
        }else{
            //不移动
        }
        return null;
    }

    //把会吃到玩家的都移除掉
    private static void removeAllEatPlayerPosition(Map<Integer,List<ChineseChessPosition>> posMap,Map<Integer,List<ChineseChessPosition>> eatPosMap){
        for (int eatChessId : eatPosMap.keySet()){
            for(ChineseChessPosition eatPos : eatPosMap.get(eatChessId)){
                if(posMap.containsKey(eatChessId)){
                    if(removeFromListIsEmpty(posMap.get(eatChessId),eatPos)){
                        posMap.remove(eatChessId);
                    }else{
                        posMap.get(eatChessId).remove(eatPos);
                    }
                }
            }
        }
    }

    private static boolean removeFromListIsEmpty(List<ChineseChessPosition> posList,ChineseChessPosition eatPos){
        List<ChineseChessPosition> removeList = new ArrayList<>();
        for(ChineseChessPosition pos : posList){
            if(pos.getX() == eatPos.getX() && pos.getY() == eatPos.getY()){
                removeList.add(pos);
            }
        }
        for(ChineseChessPosition removePos : removeList){
            posList.remove(removePos);
        }
        if(posList.size() == 0){
            return true;
        }
        return false;
    }

    //多少棋子可以吃到他
    private static int countGameEatPlayer(ChineseChessData targetChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        int countEat = 0;
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(tempChessMap);
        for(ChineseChessData chessData : tempChessMap.values()){
            if(chessData.getChessNo() == 0)continue;
            if(canEatPlayer(chessData,targetChess,posChessMap)) {
                countEat++;
            }
        }
        return countEat;
    }

    private static boolean canGameEatPlayer(Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        for(ChineseChessData chessData : chessMap.values()){
            if(chessData.getChessNo() == 0)continue;
            if(canEatPlayer(chessData,chessMap.get(0),posChessMap)) {
                return true;
            }
        }
        return false;
    }


    //第二个棋子会不会被第一个棋子吃
    private static boolean canEatPlayer(ChineseChessData eatChess,ChineseChessData targetChess,Map<Integer,Map<Integer,ChineseChessData>> posChessMap){
        List<ChineseChessPosition> eatPosList = new ArrayList<>();
        int chessId = eatChess.getChessId();
        if(chessId == eChineseChessPiecesType.Rooks.getValue()){
            for(int i = eatChess.getPosition().getX() + 1;i < x;i++){
                //挡住了，走不了了
                if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                    //是自己的棋子，直接吃掉
                    if(targetChess.getPosition().getX() == i && targetChess.getPosition().getY() == eatChess.getPosition().getY()){
                        addPosition(eatPosList,i,eatChess.getPosition().getY());
                    }
                    break;
                }
            }
            for(int i = eatChess.getPosition().getX() - 1;i >= 0;i--){
                if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                    if(targetChess.getPosition().getX() == i && targetChess.getPosition().getY() == eatChess.getPosition().getY()){
                        addPosition(eatPosList,i,eatChess.getPosition().getY());
                    }
                    break;
                }
            }
            for(int i = eatChess.getPosition().getY() + 1;i < y;i++){
                if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                    if(targetChess.getPosition().getX() == eatChess.getPosition().getX() && targetChess.getPosition().getY() == i){
                        addPosition(eatPosList,eatChess.getPosition().getX(),i);
                    }
                    break;
                }
            }
            for(int i = eatChess.getPosition().getY() - 1;i >= 0;i--){
                if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                    if(targetChess.getPosition().getX() == eatChess.getPosition().getX() && targetChess.getPosition().getY() == i){
                        addPosition(eatPosList,eatChess.getPosition().getX(),i);
                    }
                    break;
                }
            }
        }
        else if(chessId == eChineseChessPiecesType.Cannons.getValue()){
            //遇到敌人炮可以打到后面那个
            boolean meetEnemy = false;
            for(int i = eatChess.getPosition().getX() + 1;i < x;i++){
                if(!meetEnemy){
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                        meetEnemy = true;
                    }
                }else{
                    //炮打过去
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                        if(targetChess.getPosition().getX() == i && targetChess.getPosition().getY() == eatChess.getPosition().getY()){
                            addPosition(eatPosList,i,eatChess.getPosition().getY());
                        }
                        break;
                    }
                }
            }
            meetEnemy = false;
            for(int i = eatChess.getPosition().getX() - 1;i >= 0;i--){
                if(!meetEnemy){
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                        meetEnemy = true;
                    }
                }else{
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(eatChess.getPosition().getY())){
                        if(targetChess.getPosition().getX() == i && targetChess.getPosition().getY() == eatChess.getPosition().getY()){
                            addPosition(eatPosList,i,eatChess.getPosition().getY());
                        }
                        break;
                    }
                }
            }
            meetEnemy = false;
            for(int i = eatChess.getPosition().getY() + 1;i < y;i++){
                if(!meetEnemy){
                    if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                        meetEnemy = true;
                    }
                }else{
                    if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                        if(targetChess.getPosition().getX() == eatChess.getPosition().getX() && targetChess.getPosition().getY() == i){
                            addPosition(eatPosList,eatChess.getPosition().getX(),i);
                        }
                        break;
                    }
                }
            }
            meetEnemy = false;
            for(int i = eatChess.getPosition().getY() - 1;i >= 0;i--){
                if(!meetEnemy){
                    if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                        meetEnemy = true;
                    }
                }else{
                    if(posChessMap.containsKey(eatChess.getPosition().getX()) && posChessMap.get(eatChess.getPosition().getX()).containsKey(i)){
                        if(targetChess.getPosition().getX() == eatChess.getPosition().getX() && targetChess.getPosition().getY() == i){
                            addPosition(eatPosList,eatChess.getPosition().getX(),i);
                        }
                        break;
                    }
                }
            }
        }
        else{
            ChineseChessConfig chessConfig = getChessConfig(eatChess.getChessId());
            Map<Integer,ChineseChessPosition> movePositionMap =  chessConfig.getCanGoMap();
            Map<Integer,ChineseChessPosition> moveLimitMap =  chessConfig.getMoveLimitMap();
            for(int moveIndex : movePositionMap.keySet()){
                ChineseChessPosition pos = movePositionMap.get(moveIndex);
                ChineseChessPosition posLimit = moveLimitMap.get(moveIndex);
                if(canMoveNoChess(eatChess,pos)){
                    if(posLimit != null){
                        int limitMoveX = eatChess.getPosition().getX() + posLimit.getX();
                        int limitMoveY = eatChess.getPosition().getY() + posLimit.getY();
                        //被绊住不能走了
                        if(posChessMap.containsKey(limitMoveX) && posChessMap.get(limitMoveX).containsKey(limitMoveY)){
                            continue;
                        }
                    }
                    int moveX = eatChess.getPosition().getX() + pos.getX();
                    int moveY = eatChess.getPosition().getY() + pos.getY();
                    if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(moveY)){
                        if(targetChess.getPosition().getX() == moveX && targetChess.getPosition().getY() == moveY){
                            addPosition(eatPosList,moveX,moveY);
                        }
                    }
                }
            }
        }
        return eatPosList.size() > 0;
    }


    //对抗优先走有多棋子的位置且不会被吃(有车的优先去有车的地方)
    private static int meetChessNoDie(ChineseChessData playerChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        int countChe = 0;
        int count = 0;
        int moveX = playerChess.getPosition().getX();
        int moveY = playerChess.getPosition().getY();
        for(int i = 0;i < x;i++){
            if(i == moveX)continue;
            if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(moveY)){
                ChineseChessData chessData = posChessMap.get(i).get(moveY);
                if(chessData.getChessId() == eChineseChessPiecesType.Rooks.getValue()){
                    countChe++;
                }
                count++;
            }
        }
        for(int i = 0;i < y;i++){
            if(i == moveY)continue;
            if(posChessMap.containsKey(moveX) && posChessMap.get(moveX).containsKey(i)){
                ChineseChessData chessData = posChessMap.get(moveX).get(i);
                if(chessData.getChessId() == eChineseChessPiecesType.Rooks.getValue()){
                    countChe++;
                }
                count++;
            }
        }
        if(countChe > 0){
            return  -1;
        }
        return count;
    }

    //移动过程中吃掉了哪些棋子
    private static List<ChineseChessData> moveKillChessMap(int moveX,int moveY,ChineseChessData playerChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(chessMap);
        List<ChineseChessData> killChessList = new ArrayList<>();
        int playerX = playerChess.getPosition().getX();
        int playerY = playerChess.getPosition().getY();
        if(playerX == moveX){
            if(moveY > playerY){
                for(int i = playerY + 1;i <= moveY;i++){
                    if(posChessMap.containsKey(playerX) && posChessMap.get(playerX).containsKey(i)){
                        killChessList.add(posChessMap.get(playerX).get(i));
                        chessMap.remove(posChessMap.get(playerX).get(i).getChessNo());
                    }
                }
            }else{
                for(int i = playerY - 1;i >= moveY;i--){
                    if(posChessMap.containsKey(playerX) && posChessMap.get(playerX).containsKey(i)){
                        killChessList.add(posChessMap.get(playerX).get(i));
                        chessMap.remove(posChessMap.get(playerX).get(i).getChessNo());
                    }
                }
            }
        }else if(playerY == moveY){
            if(moveX > playerX){
                for(int i = playerX + 1;i <= moveX;i++){
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(playerY)){
                        killChessList.add(posChessMap.get(i).get(playerY));
                        chessMap.remove(posChessMap.get(i).get(playerY).getChessNo());
                    }
                }
            }else{
                for(int i = playerX - 1;i >= moveX;i--){
                    if(posChessMap.containsKey(i) && posChessMap.get(i).containsKey(playerY)){
                        killChessList.add(posChessMap.get(i).get(playerY));
                        chessMap.remove(posChessMap.get(i).get(playerY).getChessNo());
                    }
                }
            }
        }

        return killChessList;
    }

    private static void returnChessMap(List<ChineseChessData> killChessList,Map<Integer,ChineseChessData> chessMap){
        for(ChineseChessData killedChess : killChessList){
            chessMap.put(killedChess.getChessNo(),killedChess);
        }
    }

    public static ChineseChessPosition debugAutoMove(Map<Integer,ChineseChessData> chessMap,boolean isPowerGo){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        Map<Integer,Map<Integer,ChineseChessData>> posChessMap = getChessPosKeyMap(tempChessMap);
        Map<Integer,Map<Integer,ChineseChessPosition>> playerCanMoveMap = new ConcurrentHashMap<>();
        if(isPowerGo){
            playerCanMoveMap = getPlayerCanGoPowerMap(tempChessMap.get(0));
        }else{
            playerCanMoveMap = getSelfCanGoMap(tempChessMap.get(0),tempChessMap);
        }
        ChineseChessData playerChess = tempChessMap.get(0);

        int playerX = playerChess.getPosition().getX();
        int playerY = playerChess.getPosition().getY();
        List<ChineseChessPosition> moveEatList = new ArrayList<>();//走了能吃且没事
        List<ChineseChessPosition> moveCheList = new ArrayList<>();//有车的去找车
        List<ChineseChessPosition> moveAttackOneList = new ArrayList<>();//走了不吃且没事且可以面对一个敌人
        List<ChineseChessPosition> moveAttackMoreThanOneList = new ArrayList<>();//走了不吃且没事且可以面对两个以上敌人
        List<ChineseChessPosition> moveList = new ArrayList<>();//走了不吃且没事
        List<ChineseChessData> killChessMap = null;//假如有被吃的放这里
        for(int moveX : playerCanMoveMap.keySet()){
            for(int moveY : playerCanMoveMap.get(moveX).keySet()){
                killChessMap = moveKillChessMap(moveX,moveY,playerChess,tempChessMap);
                if(killChessMap.size() > 1 && !isPowerGo){
                    getLogger().error("出现异常，没有强化却吃了大于一个的棋子");
                }
                //模拟玩家移动了，把玩家棋子加到新的位置上
                playerChess.getPosition().setX(moveX);
                playerChess.getPosition().setY(moveY);
                //判断会不会被吃
                if(!canGameEatPlayer(tempChessMap)){
                    if(killChessMap.size() > 0){
                        moveEatList.add(new ChineseChessPosition(moveX,moveY));
                    }else{
                        int count = meetChessNoDie(playerChess,tempChessMap);
                        if(count == -1){
                            moveCheList.add(new ChineseChessPosition(moveX,moveY));
                        }else if(count > 1){
                            moveAttackMoreThanOneList.add(new ChineseChessPosition(moveX,moveY));
                        }else if(count == 1){
                            moveAttackOneList.add(new ChineseChessPosition(moveX,moveY));
                        }
                        moveList.add(new ChineseChessPosition(moveX,moveY));
                    }
                }
                playerChess.getPosition().setX(playerX);
                playerChess.getPosition().setY(playerY);
                //还原原本棋盘
                if(killChessMap.size() > 0){
                    returnChessMap(killChessMap,tempChessMap);
                    killChessMap.clear();
                }
            }
        }
        playerChess.getPosition().setX(playerX);
        playerChess.getPosition().setY(playerY);
        if(moveEatList.size() > 0){
            int r = randomHelper.next(0,moveEatList.size());
            return moveEatList.get(r);
        }
        if(moveCheList.size() > 0){
            int r = randomHelper.next(0,moveCheList.size());
            return moveCheList.get(r);
        }
        if(moveAttackMoreThanOneList.size() > 0){
            int r = randomHelper.next(0,moveAttackMoreThanOneList.size());
            return moveAttackMoreThanOneList.get(r);
        }
        if(moveAttackOneList.size() > 0){
            int r = randomHelper.next(0,moveAttackOneList.size());
            return moveAttackOneList.get(r);
        }
        if(moveList.size() > 0){
            int r = randomHelper.next(0,moveList.size());
            return moveList.get(r);
        }
        return null;
    }

    public static ChineseChessConfig getChessConfig(int chessId){
        return configChessMap.get(chessId);
    }

    public static ChineseChessConfig getChessConfigBySkinId(int skinId){
        for(ChineseChessConfig chessConfig : configChessMap.values()){
            if(chessConfig.getSkinIdList().containsKey(skinId)){
                return chessConfig;
            }
        }
        return null;
    }
    /**
     *     判断是不是要临时生成帅
     */
    public static boolean isMakeShuai(long score,int makeCount){
        for(int i = 0 ;i < scoreShuaiList.size(); i++){
            if(makeCount < i + 1 && score >= scoreShuaiList.get(i)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取可以生产帅以及他的护卫
     * @param chessNo
     * @return
     */
    public static Map<Integer,ChineseChessData> makeShuaiNextMap(int chessNo){
        Map<Integer,ChineseChessData> createMap = new ConcurrentHashMap<>();
        ChineseChessData shuaiChess = new ChineseChessData();
        shuaiChess.setChessId(eChineseChessPiecesType.King.getValue());
        shuaiChess.setChessNo(chessNo++);
        shuaiChess.setPosition(new ChineseChessPosition());
        createMap.put(shuaiChess.getChessNo(),shuaiChess);
        for(ChineseChessData attendChesss : shuaiAttendList){
            ChineseChessData chess = new ChineseChessData();
            chess.setChessId(attendChesss.getChessId());
            chess.setChessNo(chessNo++);
            chess.setPosition(new ChineseChessPosition());
            createMap.put(chess.getChessNo(),chess);
        }
        return createMap;
    }

    public static int getChessCreateNextTurn(int chessCount){
        ChineseChessCreateConfig config = getChessCreateConfig(chessCount);
        if(config == null){
            getLogger().error("象棋大逃杀新游戏生成下一次敌人的t_s_activity_chesscreate配置有问题");
            return 1;
        }
        if(config.getStep() == 0){
            return 1;
        }
        return config.getStep();
    }

    private static ChineseChessCreateConfig getChessCreateConfig(int chessCount){
        ChineseChessCreateConfig res = null;
        for(ChineseChessCreateConfig config : configChessCreateMap.values()){
            if(config.getChessNumLimit() <= chessCount){
                if(res == null || res.getChessNumLimit() < config.getChessNumLimit()){
                    res = config;
                }
            }
        }
        return res;
    }

    private static ChineseChessWeightConfig getChessWeightConfig(long score){
        ChineseChessWeightConfig res = null;
        for(ChineseChessWeightConfig config : configChessWeightMap.values()){
            if(config.getScoreLimit() <= score){
                if(res == null || res.getScoreLimit() < config.getScoreLimit()){
                    res = config;
                }
            }
        }
        return res;
    }


    /**
     * 获取配置要求的场上达到多少只不随机的棋子
     * @param chessMap
     * @return
     */
    private static  List<Integer> getNoRandomList(Map<Integer,ChineseChessData> chessMap){
        List<Integer> resList = new ArrayList<>();
        int cheCount = 0;
        int maCount = 0;
        for(ChineseChessData chess : chessMap.values()){
            if(chess.getChessNo() == 0)continue;
            if(chess.getChessId() == eChineseChessPiecesType.Rooks.getValue()){
                cheCount++;
            }else if(chess.getChessId() == eChineseChessPiecesType.Knights.getValue()){
                maCount++;
            }
        }
        if(cheCount > BIGCHESS_AICHESS_MAX_NUM_CHE){
            resList.add(eChineseChessPiecesType.Rooks.getValue());
        }
        if(maCount > BIGCHESS_AICHESS_MAX_NUM_MA){
            resList.add(eChineseChessPiecesType.Knights.getValue());
        }
        return resList;
    }

    /**
     * 随机一个棋子，要求车
     * @param score
     * @param chessNo
     * @param chessMap
     * @return
     */
    public static List<ChineseChessData> randomNextChessList(long score, int chessNo, Map<Integer,ChineseChessData> chessMap){
        List<ChineseChessData> randomRes = new ArrayList<>();
        ChineseChessCreateConfig createConfig = ChineseChessMgr.getChessCreateConfig(chessMap.size());
        if(createConfig == null){
            getLogger().error("象棋大逃杀新游戏生成下一次敌人的t_s_activity_chesscreate配置有问题");
            return randomRes;
        }
        ChineseChessWeightConfig weightConfig = getChessWeightConfig(score);
        if(weightConfig == null){
            getLogger().error("象棋大逃杀新游戏生成下一次敌人的t_s_activity_chessweight配置有问题");
            return randomRes;
        }
        List<Integer> noRandomList = getNoRandomList(chessMap);
        for(int i = 0;i < createConfig.getCreateNum(); i++){
           int chessId = randomChess(weightConfig,noRandomList);
            ChineseChessData chessData = new ChineseChessData();
            chessData.setChessNo(chessNo++);
            chessData.setChessId(chessId);
            chessData.setPosition(new ChineseChessPosition());
            randomRes.add(chessData);
        }

        return randomRes;
    }

    /**
     * 随机一个棋子，有一些棋子不随机剔除掉
     * @param config
     * @param noRandomList 有些棋子满了就不随机出来了
     * @return
     */
    private static int randomChess(ChineseChessWeightConfig config,List<Integer> noRandomList){
        Map<Integer,Integer> weightMap = new ConcurrentHashMap<>(config.getWeightMap());
        for(int chessId : noRandomList){
            weightMap.remove(chessId);
        }
        int totalWeight = 0;
        for (Integer weight : weightMap.values()) {
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (Integer chessId : weightMap.keySet()) {
            int value = weightMap.get(chessId);
            if (randomNum < value) {
                return chessId;
            }
            randomNum -= value;
        }
        return 0;
    }

    /**
     * 初始化自己的位置
     * @return
     */
    public static ChineseChessPosition getSelfPosition(){
        ChineseChessPosition chessPosition = new ChineseChessPosition();
        chessPosition.setX(initialSelfPosition.getX());
        chessPosition.setY(initialSelfPosition.getY());
        return chessPosition;
    }

    /**
     * 初始化的敌人
     * @return
     */
    public static List<ChineseChessData> getInitialEnemyChessList(){
        List<ChineseChessData> res = new ArrayList<>();
        for(ChineseChessData chess : initialEnemyChessList){
            ChineseChessData tempChess = new ChineseChessData();
            tempChess.setChessNo(chess.getChessNo());
            tempChess.setChessId(chess.getChessId());
            ChineseChessPosition tempPosition = new ChineseChessPosition();
            tempPosition.setX(chess.getPosition().getX());
            tempPosition.setY(chess.getPosition().getY());
            tempChess.setPosition(tempPosition);
            res.add(tempChess);
        }
        return res;
    }

    /**
     * 先按照每回合生成一个随机位置的随机棋子做
     * @param chessMap
     * @param nextChessMap
     * @param chessNo
     * @return
     */
    public static ChineseChessData randomChess(Map<Integer,ChineseChessData> chessMap,Map<Integer,ChineseChessData> nextChessMap,int chessNo){
        ChineseChessData newOne = new ChineseChessData();
        newOne.setChessNo(chessNo);
        ChineseChessPosition position = new ChineseChessPosition();
        List<ChineseChessConfig> ChineseChessConfigList = new ArrayList<>(configChessMap.values());
        int chessSize = ChineseChessConfigList.size();
        int randomNum = randomHelper.next(0, chessSize);
        ChineseChessConfig chessConfig = ChineseChessConfigList.get(randomNum);
        newOne.setChessId(chessConfig.getId());
        List<ChineseChessPosition> surplusChessList = getSurplusChessPositionList(chessMap,nextChessMap);
        if(surplusChessList == null){
            return null;
        }
        int surplusSize = surplusChessList.size();
        int randomPositionNum = randomHelper.next(0, surplusSize);
        ChineseChessPosition randomPos = surplusChessList.get(randomPositionNum);
        position.setX(randomPos.getX());
        position.setY(randomPos.getY());
        newOne.setPosition(position);
        return newOne;
    }

    /**
     * 随机一个棋子的位置
     * @param chessMap
     * @param nextChess
     * @return
     */
    public static boolean randomNextChessPosition(Map<Integer,ChineseChessData> chessMap, ChineseChessData nextChess){
        ChineseChessPosition position = randomChessPosition(chessMap);
        if(position == null){
            return false;
        }
        nextChess.getPosition().setX(position.getX());
        nextChess.getPosition().setY(position.getY());
        return true;
    }

    //走到哪一步会生成多少棋子，大于配置就是倒数第二个的和倒数第一个的差值的步数一直无限延续最后一个的配置，假如差值是5，就是每走5步生成最后一个的配置棋子
    public static ChineseChessMoveConfig getChessMoveConfig(int step){
        if(configChessMoveMap.containsKey(step)){
            return configChessMoveMap.get(step);
        }
        List<Integer> stepList = new ArrayList<>(configChessMoveMap.keySet());
        Collections.sort(stepList);
        int maxStepValue = stepList.get(stepList.size() - 1);
        int secMaxStepValue = stepList.get(stepList.size() - 2);
        if(step > maxStepValue){
            if((step - maxStepValue)%(maxStepValue - secMaxStepValue) == 0){
                return configChessMoveMap.get(maxStepValue);
            }
        }
        return null;
    }

    public static ChineseChessMoveConfig getChessMoveDebugConfig(int step){
        ChineseChessMoveConfig chessMoveConfig = getChessMoveConfig(step);
        if(chessMoveConfig != null){
            return chessMoveConfig;
        }
        if(configChessMoveMap.containsKey(step)){
            return configChessMoveMap.get(step);
        }
        List<Integer> stepList = new ArrayList<>(configChessMoveMap.keySet());
        Collections.sort(stepList);
        int maxStepValue = stepList.get(stepList.size() - 1);
        for(int i = 0 ; i < stepList.size() ;i++){
            if(stepList.get(i) > step){
                return configChessMoveMap.get(stepList.get(i));
            }
        }
        if(step > maxStepValue){
            return configChessMoveMap.get(maxStepValue);
        }
        return null;
    }

    private static ChineseChessMoveConfig getChessNextMoveConfig(int step){
        List<Integer> stepList = new ArrayList<>(configChessMoveMap.keySet());
        Collections.sort(stepList);
        int maxStepValue = stepList.get(stepList.size() - 1);
        if(step >= maxStepValue){
            return null;
//            return configChessMoveMap.get(maxStepValue);
        }else{
            for(int i = 0;i < stepList.size();i++){
                if(stepList.get(i) > step){
                    return configChessMoveMap.get(stepList.get(i));
                }
            }
        }
        return null;
    }

    public static int getNextChessTurn(int step){
        List<Integer> stepList = new ArrayList<>(configChessMoveMap.keySet());
        Collections.sort(stepList);
        int maxStepValue = stepList.get(stepList.size() - 1);
        int secMaxStepValue = stepList.get(stepList.size() - 2);
        if(step == maxStepValue){
            return maxStepValue - secMaxStepValue;
        }
        if(step > maxStepValue){
            return (maxStepValue - secMaxStepValue) - (step - maxStepValue)%(maxStepValue - secMaxStepValue);
        }else{
            for(int i = 0;i < stepList.size();i++){
                if(stepList.get(i) > step){
                    return stepList.get(i) - step;
                }
            }
        }
        return 1;
    }

    public static Map<Integer,ChineseChessData> getNextChessMap(int step,int chessNo){
        ChineseChessMoveConfig chessMoveConfig = getChessNextMoveConfig(step);
        if(chessMoveConfig == null){
            return null;
        }
        Map<Integer,ChineseChessData> chessMap = new ConcurrentHashMap<>();
        for(int i = 0;i < chessMoveConfig.getChessList().size(); i++){
            int chessId = chessMoveConfig.getChessList().get(i);
            ChineseChessData chessData = new ChineseChessData();
            chessData.setChessNo(chessNo++);
            chessData.setChessId(chessId);
            chessData.setPosition(new ChineseChessPosition());
            chessMap.put(chessData.getChessNo(),chessData);
        }
        return chessMap;
    }

    //新版随机位置
    private static ChineseChessPosition randomNewVersionPosition(Map<ChineseChessPosition,Integer> posWeightMap){
        ChineseChessPosition randomPos = null;
        int totalWeight = 0;
        for (int weight : posWeightMap.values()) {
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (ChineseChessPosition pos : posWeightMap.keySet()) {
            int value = posWeightMap.get(pos);
            if (randomNum < value) {
                randomPos = pos;
                break;
            }
            randomNum -= value;
        }
        return randomPos;
    }

    private static boolean checkMapZero(Map<ChineseChessPosition,Integer> posWeightMap){
        if(posWeightMap == null){
            return false;
        }
        for(Integer weight : posWeightMap.values()){
            if(weight != 0){
                return false;
            }
        }
        for(ChineseChessPosition pos : posWeightMap.keySet()){
            posWeightMap.put(pos,1);
        }
        return true;
    }

    private static ChineseChessData makeOneNextChessPos(ChineseChessMoveConfig chessMoveConfig,ChineseChessData nextChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        Map<ChineseChessPosition,Integer> posWeightMap = getPosWeightMapVersion3(chessMoveConfig,nextChess,tempChessMap);
        //没位置生成棋子了
        if(posWeightMap == null){
            return null;
        }
        if(checkMapZero(posWeightMap)){
            getLogger().error("象棋大逃杀出现了落地权重全部为零的情况");
        }
        ChineseChessPosition pos = randomNewVersionPosition(posWeightMap);
        if(pos == null){
            getLogger().error("象棋大逃杀出现随机位置为空的情况");
            return null;
        }
        nextChess.setPosition(pos);
        tempChessMap.put(nextChess.getChessNo(),nextChess);
        return nextChess;
    }

    public static Map<Integer,ChineseChessData> makeNextChessPos(int step,Map<Integer,ChineseChessData> nextChessMap,Map<Integer,ChineseChessData> chessMap){
        ChineseChessMoveConfig chessMoveConfig = getChessMoveConfig(step);
        if(chessMoveConfig == null){
            getLogger().error("象棋大逃杀生成棋子配置异常或者没有");
            return null;
        }
        List<Integer> removeChessIdList = new ArrayList<>();
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        for(ChineseChessData nextChess : nextChessMap.values()){
            ChineseChessData resChess = makeOneNextChessPos(chessMoveConfig,nextChess,tempChessMap);
            if(resChess == null){
                removeChessIdList.add(nextChess.getChessNo());
            }else{
                tempChessMap.put(resChess.getChessNo(),resChess);
            }
        }
        for(Integer chessNo : removeChessIdList){
            nextChessMap.remove(chessNo);
        }
        return nextChessMap;
    }



    //生成帅的，有士要去保护他
    public static Map<Integer,ChineseChessData> makeNextChessPosKing(int step,Map<Integer,ChineseChessData> nextChessMap,Map<Integer,ChineseChessData> chessMap){
        ChineseChessMoveConfig chessMoveConfig = getChessMoveConfig(step);
        if(chessMoveConfig == null){
            getLogger().error("象棋大逃杀生成棋子配置异常或者没有");
            return null;
        }
        ChineseChessData kingChess = null;
        List<ChineseChessData> shiChessList = new ArrayList<>();
        List<ChineseChessData> otherChessList = new ArrayList<>();
        for(ChineseChessData chessData : nextChessMap.values()){
            if(chessData.getChessId() == eChineseChessPiecesType.King.getValue()){
                kingChess = chessData;
            }else if(chessData.getChessId() == eChineseChessPiecesType.Mandarins.getValue()){
                shiChessList.add(chessData);
            }else{
                otherChessList.add(chessData);
            }
        }
        List<Integer> removeChessIdList = new ArrayList<>();
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        if(kingChess != null){
            ChineseChessData resChess = makeOneNextChessPos(chessMoveConfig,kingChess,tempChessMap);
            if(resChess == null){
                removeChessIdList.add(kingChess.getChessNo());
            }else{
                tempChessMap.put(resChess.getChessNo(),resChess);
            }
        }
        for(ChineseChessData shiChess : shiChessList){
            ChineseChessPosition pos = getKingAttendChessPosition(tempChessMap,kingChess,shiChess);
            if(pos != null){
                shiChess.setPosition(pos);
                tempChessMap.put(shiChess.getChessNo(),shiChess);
            }else{
                ChineseChessData resChess = makeOneNextChessPos(chessMoveConfig,shiChess,tempChessMap);
                if(resChess == null){
                    removeChessIdList.add(shiChess.getChessNo());
                }else{
                    tempChessMap.put(resChess.getChessNo(),resChess);
                }
            }
        }
        for(ChineseChessData nextChess : otherChessList){
            ChineseChessData resChess = makeOneNextChessPos(chessMoveConfig,nextChess,tempChessMap);
            if(resChess == null){
                removeChessIdList.add(nextChess.getChessNo());
            }else{
                tempChessMap.put(resChess.getChessNo(),resChess);
            }
        }
        for(Integer chessNo : removeChessIdList){
            nextChessMap.remove(chessNo);
        }
        return nextChessMap;
    }

    public static Map<ChineseChessPosition,Integer> getPosWeightMap(ChineseChessMoveConfig chessMoveConfig,ChineseChessData makeChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        List<ChineseChessPosition> surplusChessList = getSurplusChessPositionList(chessMap);
        if(surplusChessList == null){
            getLogger().error("象棋大逃杀没有位置生成棋子");
            return null;
        }
        Map<ChineseChessPosition,Integer> weightMap = new ConcurrentHashMap<>();
        /*for(ChineseChessPosition pos : surplusChessList){
            if(pos.getX() == 4 && pos.getY() == 7){
                makeChess.setPosition(pos);
                tempChessMap.put(makeChess.getChessNo(),makeChess);
                int passRoadValue = getPassRoadValue(tempChessMap);
                int couplingValue = getAllCouplingValue(tempChessMap);
                int weight = makeWeightValue(chessMoveConfig,passRoadValue,couplingValue);
                weightMap.put(pos,weight);
            }
        }*/
        for(ChineseChessPosition makePos : surplusChessList){
            makeChess.setPosition(makePos);
            tempChessMap.put(makeChess.getChessNo(),makeChess);
            int passRoadValue = getPassRoadValue(tempChessMap);
            int couplingValue = getAllCouplingValue(tempChessMap);
            int weight = makeWeightValue(chessMoveConfig,passRoadValue,couplingValue);
            weightMap.put(makePos,weight);
        }
        return weightMap;
    }

    //公式各种开根号次方注释没法写去看文档
    private static int makeWeightValue(ChineseChessMoveConfig chessMoveConfig,int passRoadValue,int couplingValue){
        int p = passRoadValue;
        int up = chessMoveConfig.getAccessExpected();
        int tp = tParamList.get(0);
        int op = tParamList.get(1);
        int rp = tParamList.get(2);

        int q = couplingValue;
        int uq = chessMoveConfig.getCouplingExpected();
        int tq = oParamList.get(0);
        int oq = oParamList.get(1);
        int rq = oParamList.get(2);

        // wp计算公式 W_p=t_p/√2πσ_pe^− (p−μ_p)^2/r_pσ_p^2
        // t_p/√2πσ_p
        // 分子BigDecimal.valueOf(tp);
        // 分母BigDecimal.valueOf(Math.sqrt(2 * Math.PI)).multiply(BigDecimal.valueOf(op));
        //− (p−μ_p)^2/r_pσ_p^2
        // 分子BigDecimal.valueOf(Math.pow(BigDecimal.valueOf(p).subtract(BigDecimal.valueOf(up)).doubleValue(),2));
        // 分母BigDecimal.valueOf(rp).multiply(BigDecimal.valueOf(Math.pow(op,2)));
        BigDecimal a = BigDecimal.valueOf(tp).divide(BigDecimal.valueOf(Math.sqrt(2 * Math.PI)).multiply(BigDecimal.valueOf(op)),8,BigDecimal.ROUND_HALF_UP);
        BigDecimal b = BigDecimal.valueOf(Math.pow(BigDecimal.valueOf(p).subtract(BigDecimal.valueOf(up)).doubleValue(),2))
                .divide(BigDecimal.valueOf(rp).multiply(BigDecimal.valueOf(Math.pow(op,2))),8,BigDecimal.ROUND_HALF_UP);
        BigDecimal wp = BigDecimal.valueOf(tp)
                .divide(BigDecimal.valueOf(Math.sqrt(2 * Math.PI)).multiply(BigDecimal.valueOf(op)),8,BigDecimal.ROUND_HALF_UP)
                .multiply(BigDecimal.valueOf(Math.pow(Math.E,
                        -BigDecimal.valueOf(Math.pow(BigDecimal.valueOf(p).subtract(BigDecimal.valueOf(up)).doubleValue(),2))
                .divide(BigDecimal.valueOf(rp).multiply(BigDecimal.valueOf(Math.pow(op,2))),8,BigDecimal.ROUND_HALF_UP).doubleValue())));
        // W_q=t_q/√2πσ_qe^− (q−μ_q)^2/r_qσ_q^2
        // t_q/√2πσ_q
        // 分子BigDecimal.valueOf(tq);
        // 分母BigDecimal.valueOf(Math.sqrt(2 * Math.PI)).multiply(BigDecimal.valueOf(oq));
        //− (q−μ_q)^2/r_qσ_q^2
        // 分子BigDecimal.valueOf(Math.pow(BigDecimal.valueOf(q).subtract(BigDecimal.valueOf(uq)).doubleValue(),2));
        // 分母BigDecimal.valueOf(rq).multiply(BigDecimal.valueOf(Math.pow(oq,2)));
        BigDecimal wq = BigDecimal.valueOf(tq)
                .divide(BigDecimal.valueOf(Math.sqrt(2 * Math.PI)).multiply(BigDecimal.valueOf(oq)),8,BigDecimal.ROUND_HALF_UP)
                .multiply(BigDecimal.valueOf(Math.pow(Math.E,
                        -BigDecimal.valueOf(Math.pow(BigDecimal.valueOf(q).subtract(BigDecimal.valueOf(uq)).doubleValue(),2))
                                .divide(BigDecimal.valueOf(rq).multiply(BigDecimal.valueOf(Math.pow(oq,2))),8,BigDecimal.ROUND_HALF_UP).doubleValue())));
        int weight = wp.add(wq).setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
        if(weight == 0){
            return 1;
        }
        return weight;
    }


    public static Map<ChineseChessPosition,Integer> getPosWeightMapVersion3(ChineseChessMoveConfig chessMoveConfig,ChineseChessData makeChess,Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        List<ChineseChessPosition> surplusChessList = getSurplusChessPositionList(chessMap);
        if(surplusChessList == null){
            getLogger().error("象棋大逃杀没有位置生成棋子");
            return null;
        }
        Map<ChineseChessPosition,ChessK> k1Map = new ConcurrentHashMap<>();
        Map<ChineseChessPosition,ChessK> k2Map = new ConcurrentHashMap<>();
        for(ChineseChessPosition makePos : surplusChessList){
            makeChess.setPosition(makePos);
            tempChessMap.put(makeChess.getChessNo(),makeChess);
            int passRoadValue = getPassRoadValue(tempChessMap);
            int couplingValue = getAllCouplingValue(tempChessMap);
            ChessK k1 = makeWeightValueVersion3SimpleK(passRoadValue,chessMoveConfig.getAccessExpected());
            ChessK k2 = makeWeightValueVersion3SimpleK(couplingValue,chessMoveConfig.getCouplingExpected());
            k1.setPos(makePos);
            k2.setPos(makePos);
            k1Map.put(makePos,k1);
            k2Map.put(makePos,k2);
        }
        Map<ChineseChessPosition,Integer> weightMap = kToWeightMapSimpleK(k1Map,k2Map);

        return weightMap;
    }
    //第三版策划公式
    private static ChessK makeWeightValueVersion3SimpleK(int value1,int value2){
        ChessK k = new ChessK();
        k.setkValue(Math.abs(value1 - value2));
        return k;
    }

    private static ChessK makeWeightValueVersion3(ChineseChessMoveConfig chessMoveConfig,int passRoadValue,int couplingValue){
        double p = passRoadValue;
        double up = chessMoveConfig.getAccessExpected();
        double q = couplingValue;
        double uq = chessMoveConfig.getCouplingExpected();

        double t = paramList.get(0);
        ChessK k = new ChessK();
        k.setkValue(Math.abs(p - up) + Math.abs(q - uq) * t /1000);
        return k;
    }

    private static Map<ChineseChessPosition,Integer> kToWeightMap(Map<ChineseChessPosition,ChessK> kMap){
        Map<ChineseChessPosition,Integer> weightMap = new ConcurrentHashMap<>();
        double a = paramList.get(1);
        double b = paramList.get(2);
        double c = paramList.get(3);

        List<ChessK> kList = new ArrayList<>(kMap.values());
        kList.sort(new KSort());


        for(int r = 1; r <= kList.size();r++){
            ChessK k = kList.get(r - 1);
            int weight = BigDecimal.valueOf(Math.pow(Math.max(a - r,0),b/1000) + c).setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
            weightMap.put(k.getPos(),weight);
        }
        return weightMap;
    }

    private static Map<ChineseChessPosition,Integer> kToWeightMapSimpleK(Map<ChineseChessPosition,ChessK> k1Map,Map<ChineseChessPosition,ChessK> k2Map){
        Map<ChineseChessPosition,Integer> weightMap = new ConcurrentHashMap<>();
        Map<ChineseChessPosition,Double> weight1Map = new ConcurrentHashMap<>();
        Map<ChineseChessPosition,Double> weight2Map = new ConcurrentHashMap<>();
        double t = paramList.get(0);
        double a = paramList.get(1);
        double b = paramList.get(2);
        double c = paramList.get(3);

        List<ChessK> k1List = new ArrayList<>(k1Map.values());
        k1List.sort(new KSort());

        List<ChessK> k2List = new ArrayList<>(k2Map.values());
        k2List.sort(new KSort());

        int lastRank = -1;
        double lastKValue = -1;
        for(int r = 1; r <= k1List.size();r++){
            ChessK k = k1List.get(r - 1);
            int rank = r;
            if(lastKValue == k.getkValue()){
                rank = lastRank;
            }else{
                lastRank = r;
                lastKValue = k.getkValue();
            }
            double weight = BigDecimal.valueOf(Math.pow(Math.max(a - rank,0),b/1000) + c).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            weight1Map.put(k.getPos(),weight);
        }

        lastRank = -1;
        lastKValue = -1;
        for(int r = 1; r <= k2List.size();r++){
            ChessK k = k2List.get(r - 1);
            int rank = r;
            if(lastKValue == k.getkValue()){
                rank = lastRank;
            }else{
                lastRank = r;
                lastKValue = k.getkValue();
            }
            double weight = BigDecimal.valueOf(Math.pow(Math.max(a - rank,0),b/1000) + c).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            weight2Map.put(k.getPos(),weight);
        }
        for(ChineseChessPosition pos : weight1Map.keySet()){
            int weight = BigDecimal.valueOf(weight1Map.get(pos) + weight2Map.get(pos) * t / 1000).setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
            weightMap.put(pos,weight);
        }
        return weightMap;
    }

    private static class KSort implements Comparator<ChessK>{
        @Override
        public int compare(ChessK o1, ChessK o2) {
            if (o1.getkValue() == o2.getkValue()) {
                return 0;
            }
            if (o1.getkValue() >= o2.getkValue()) {
                return 1;
            }
            return -1;
        }
    }

    private static int getCouplingValue(int eatCount){
        if(eatCount == 0){
            return 0;
        }
        if(eatCount > oValueList.size()){
            return oValueList.get(oValueList.size() - 1);
        }
        return oValueList.get(eatCount - 1);
    }

    //计算盘面的耦合数
    public static int getAllCouplingValue(Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> noPlayerChessMap = new ConcurrentHashMap<>(chessMap);
        ChineseChessData playerChess = noPlayerChessMap.get(0);
        noPlayerChessMap.remove(0);
        int couplingValue = 0;
        for(ChineseChessData chessData : noPlayerChessMap.values()){
            int eatCount = countGameEatPlayer(chessData,noPlayerChessMap);
            couplingValue = couplingValue + getCouplingValue(eatCount);
        }
        return couplingValue;
    }

    /**
     * 获取棋盘通路值
     * @param chessMap
     * @return
     */
    public static int getPassRoadValue(Map<Integer,ChineseChessData> chessMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        ChineseChessData playerChess = tempChessMap.get(0);
        int playerX = playerChess.getPosition().getX();
        int playerY = playerChess.getPosition().getY();
        Map<Integer,Map<Integer,ChineseChessPosition>> moveMap = new ConcurrentHashMap<>();
        goCanEatNoBeEat(playerChess,tempChessMap,moveMap);
        playerChess.getPosition().setX(playerX);
        playerChess.getPosition().setY(playerY);
        int moveCount = 0;
        for(Map<Integer,ChineseChessPosition> moveYMap : moveMap.values()){
            moveCount += moveYMap.size();
        }
        return moveCount;
    }

    //走了可吃且不会被吃,然后接下去不能吃且不会被吃的通路数
    private static Map<Integer,Map<Integer,ChineseChessPosition>> goCanEatNoBeEat(ChineseChessData playerChess,Map<Integer,ChineseChessData> chessMap,Map<Integer,Map<Integer,ChineseChessPosition>> moveMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        Map<Integer,Map<Integer,ChineseChessPosition>> playerCanMoveMap = getSelfCanGoMap(playerChess,tempChessMap);
        List<ChineseChessPosition> moveList = new ArrayList<>();//走了不吃且没事
        for(int moveX : playerCanMoveMap.keySet()){
            for(int moveY : playerCanMoveMap.get(moveX).keySet()){
                playerChess.setPosition(playerCanMoveMap.get(moveX).get(moveY));
                //判断会不会被吃
                if(!canGameEatPlayer(tempChessMap)){
                    if(!moveMap.containsKey(moveX) || !moveMap.get(moveX).containsKey(moveY)){
                        moveList.add(new ChineseChessPosition(moveX,moveY));
                    }
                }
            }
        }
        addPosKeyMap(moveMap,moveList);
        /*ChineseChessData removeChess = null;
        for(ChineseChessPosition movPos : moveList){
            for(ChineseChessData chess : tempChessMap.values()){
                if(chess.getChessNo() != 0 && chess.getPosition().getX() == movPos.getX() && chess.getPosition().getY() == movPos.getY()){
                    removeChess = chess;
                    tempChessMap.remove(chess.getChessNo());
                }
            }
            playerChess.setPosition(movPos);
            goNoEatNoBeEat(playerChess,tempChessMap,moveMap);
            if(removeChess != null){
                tempChessMap.put(removeChess.getChessNo(),removeChess);
                removeChess = null;
            }
        }*/
        return moveMap;
    }

    //走了不吃且不会被吃
    private static Map<Integer,Map<Integer,ChineseChessPosition>> goNoEatNoBeEat(ChineseChessData playerChess,Map<Integer,ChineseChessData> chessMap,Map<Integer,Map<Integer,ChineseChessPosition>> moveMap){
        Map<Integer,ChineseChessData> tempChessMap = new ConcurrentHashMap<>(chessMap);
        Map<Integer,Map<Integer,ChineseChessPosition>> playerCanMoveMap = getSelfCanGoNoEatMap(playerChess,tempChessMap);
        List<ChineseChessPosition> moveList = new ArrayList<>();//走了不吃且没事
        for(int moveX : playerCanMoveMap.keySet()){
            for(int moveY : playerCanMoveMap.get(moveX).keySet()){
                playerChess.setPosition(playerCanMoveMap.get(moveX).get(moveY));
                //判断会不会被吃
                if(!canGameEatPlayer(tempChessMap)){
                    if(!moveMap.containsKey(moveX) || !moveMap.get(moveX).containsKey(moveY)){
                        moveList.add(new ChineseChessPosition(moveX,moveY));
                    }
                }
            }
        }
        addPosKeyMap(moveMap,moveList);
        for(ChineseChessPosition movPos : moveList){
            playerChess.setPosition(movPos);
            goNoEatNoBeEat(playerChess,tempChessMap,moveMap);
        }
        return moveMap;
    }

    public static ChineseChessPosition randomChessPosition(Map<Integer,ChineseChessData> chessMap){
        ChineseChessPosition position = new ChineseChessPosition();
        List<ChineseChessPosition> surplusChessList = getSurplusChessPositionList(chessMap);
        if(surplusChessList == null){
            return null;
        }
        int surplusSize = surplusChessList.size();
        int randomPositionNum = randomHelper.next(0, surplusSize);
        ChineseChessPosition randomPos = surplusChessList.get(randomPositionNum);
        position.setX(randomPos.getX());
        position.setY(randomPos.getY());
        return position;
    }

    /**
     * 设置好帅的位置，和护卫的位置，护卫的位置是能保护帅，也就是当帅被吃了，护卫立马吃了你
     * @param chessMap
     * @param nextChessMap
     * @return
     */
    public static boolean makeNextKingPosition(Map<Integer,ChineseChessData> chessMap,Map<Integer,ChineseChessData> nextChessMap){
        ChineseChessData kingChess = null;
        for(ChineseChessData chessData : nextChessMap.values()){
            if(chessData.getChessId() == eChineseChessPiecesType.King.getValue()){
                kingChess = chessData;
                if(!randomNextChessPosition(chessMap,kingChess)){
                    return false;
                }
            }
        }
        if(kingChess == null){
            return false;
        }
        List<Integer> removeList = new ArrayList<>();
        //已经随机了的位置已经没了
        Map<Integer,ChineseChessData> addMap = new ConcurrentHashMap<>();
        Map<Integer,ChineseChessData> newChessMap = new ConcurrentHashMap<>(chessMap);
        for(ChineseChessData chessData : nextChessMap.values()){
            newChessMap.putAll(addMap);
            if(chessData.getChessId() != eChineseChessPiecesType.King.getValue()){
                ChineseChessPosition pos = getKingAttendChessPosition(newChessMap,kingChess,chessData);
                if(pos != null){
                    chessData.setPosition(pos);
                    addMap.put(chessData.getChessNo(),chessData);
                }else{
                    if(!randomNextChessPosition(newChessMap,chessData)){
                        removeList.add(chessData.getChessNo());
                        addMap.put(chessData.getChessNo(),chessData);
                    }
                }
            }
        }
        for(Integer chessNo : removeList){
            nextChessMap.remove(chessNo);
        }
        return true;
    }

    /**
     * 把还是空位可以走的位置返回
     * @param chessMap
     * @param nextChessMap
     * @return
     */
    private static List<ChineseChessPosition> getSurplusChessPositionList(Map<Integer,ChineseChessData> chessMap, Map<Integer,ChineseChessData>  nextChessMap){
        Map<Integer,ChineseChessData> allMap = new ConcurrentHashMap<>(chessMap);
        allMap.putAll(nextChessMap);
        return getSurplusChessPositionList(allMap);
    }

    /**
     * 把还是空位可以走的位置返回
     * @param chessMap
     * @return
     */
    private static List<ChineseChessPosition> getSurplusChessPositionList(Map<Integer,ChineseChessData> chessMap){
        if(chessMap.size() == allPositionSimpleMap.size()){
            return null;
        }
        Map<String,ChineseChessPosition> allSimpleMap = new ConcurrentHashMap<>(allPositionSimpleMap);
        for(ChineseChessData chess : chessMap.values()){
            String key = chess.getPosition().getX() + "-" + chess.getPosition().getY();
            allSimpleMap.remove(key);
        }
        return new ArrayList<>(allSimpleMap.values());
    }

    /**
     * 获取帅的护卫的位置，能直接吃帅的位置，如果没有位置了，就随机一个空位
     * @param chessMap
     * @param kingChess
     * @param attendChess
     * @return
     */
    private static ChineseChessPosition getKingAttendChessPosition(Map<Integer,ChineseChessData> chessMap,ChineseChessData kingChess,ChineseChessData attendChess){
        if(kingChess == null){
            return null;
        }
        List<String> canGoList = new ArrayList<>();
        List<ChineseChessPosition> canGoPosList = getSurplusChessPositionList(chessMap);
        if(canGoPosList == null){
            return null;
        }
        for(ChineseChessPosition pos : canGoPosList){
            canGoList.add(pos.getX() + "-" + pos.getY());
        }
        ChineseChessConfig attendConfig = getChessConfig(attendChess.getChessId());
        //todo-xx 炮护卫和车护卫要特殊处理
        if(attendConfig.getId() == eChineseChessPiecesType.Cannons.getValue()){
            return randomChessPosition(chessMap);
        }else if(attendConfig.getId() == eChineseChessPiecesType.Rooks.getValue()){
            return randomChessPosition(chessMap);
        }else{
            //只会配士，不用看会不会绊到了
            for(ChineseChessPosition pos : attendConfig.getCanGoMap().values()){
                //移动在地图范围里
                if(canMoveNoChess(kingChess,pos)){
                    ChineseChessPosition movePos = new ChineseChessPosition();
                    movePos.setX(kingChess.getPosition().getX() + pos.getX());
                    movePos.setY(kingChess.getPosition().getY() + pos.getY());
                    if(canGoList.contains(movePos.getX() + "-" + movePos.getY())){
                        return movePos;
                    }
                }
            }
        }
        return null;
    }

    //开启强化效果沿途位置
    public static Map<String,ChineseChessPosition> getPowerOnMovePosMap(ChineseChessData playerChess,int moveX,int moveY){
        Map<String,ChineseChessPosition> map = new ConcurrentHashMap<>();
        if(playerChess.getPosition().getX() == moveX){
            if(moveY > playerChess.getPosition().getY()){
                for(int i = playerChess.getPosition().getY() + 1;i <= moveY;i++){
                    map.put(moveX + "-" + i, new ChineseChessPosition(moveX,i));
                }
            }else{
                for(int i = moveY;i < playerChess.getPosition().getY();i++){
                    map.put(moveX + "-" + i,new ChineseChessPosition(moveX,i));
                }
            }
        }else if(playerChess.getPosition().getY() == moveY){
            if(moveX > playerChess.getPosition().getX()){
                for(int i = playerChess.getPosition().getX() + 1;i <= moveX;i++){
                    map.put(i + "-" + moveY,new ChineseChessPosition(i,moveY));
                }
            }else{
                for(int i = moveX;i < playerChess.getPosition().getX();i++){
                    map.put(i + "-" + moveY,new ChineseChessPosition(i,moveY));
                }
            }
        }
        return map;
    }

    public static Map<String,ChineseChessPosition> getKingPowerOnEatPosMap(int moveX,int moveY){
        Map<String,ChineseChessPosition> map = new ConcurrentHashMap<>();
        for(int i = 0;i < x;i++){
            map.put(i + "-" + moveY,new ChineseChessPosition(i,moveY));
        }
        for(int i = 0;i < y;i++){
            map.put(moveX + "-" + i,new ChineseChessPosition(moveX,i));
        }
        return map;
    }

    public static int getGameOverStep(){
        return BIGCHESS_WIN_ROUND;
    }

    public static Property getFirstPassReward(){
        return PropertyHelper.parseStringToProperty(BIGCHESS_WIN_REWARD);
    }

    public static Property getGameOverReward(int score){
        List<Integer> scoreList = new ArrayList<>(gameOverRewardMap.keySet());
        Collections.sort(scoreList);
        for(int i = 0; i < scoreList.size() ; i ++){
            if(score < scoreList.get(i)){
                return PropertyHelper.parseStringToProperty(gameOverRewardMap.get(scoreList.get(i)));
            }
        }
        return new Property();
    }

    public static void sendGameOverReward(){
        long nowTime = System.currentTimeMillis() / 1000;
        if(nowTime - activityInfo.getEndTime() > 300 && nowTime < activityInfo.getEndShowTime()){
            if(!isSendRewardMap.containsKey(activityInfo.getActivityId()) || isSendRewardMap.get(activityInfo.getActivityId())){
                Map<Long,ChineseChessUserData> userDataMap = ChineseChessBusiness.getSendRewardData(activityInfo.getActivityId());
                for(ChineseChessUserData userData : userDataMap.values()){
                    userData.setGameStatus(eChineseChessGameStatusType.Over.getValue());
                    if(userData.getScore().longValue() > userData.getTopScore().longValue()){
                        userData.setTopScore(userData.getScore());
                    }
                    Property activityOverReward = new Property();
                    if(userData.getPassTime() == 1 && userData.getGameStatus() == eChineseChessGameStatusType.Pass.getValue()){
                        Property firstReward = ChineseChessMgr.getFirstPassReward();
                        activityOverReward.addProperty(firstReward);
                    }
                    Property gameOverReward = ChineseChessMgr.getGameOverReward(userData.getScore().intValue());
                    activityOverReward.addProperty(gameOverReward);
//                    player.getModule(CurrencyModule.class).addCurrency(gameOverReward, eLogMoneyType.ChineseChessActivity,eLogMoneyType.ChineseChessActivityOverReward);
                    UserInfo userInfo = UserMgr.getUserInfo(userData.getUserId());
                    if(userInfo == null){
                        getLogger().error(userData.getUserId() + "找不到userInfo活动结束没有发象棋大逃杀对局奖励");
                    }else{
                        String language = userInfo.getLanguage();
                        String activityName = ActivityMgr.getActivityName(activityInfo, language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.CHESS_ACTIVITY_OVER_GAME_REWARD_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.CHESS_ACTIVITY_OVER_GAME_REWARD_CONTENT, language), activityName);
                        MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(activityOverReward), mailContent, mailTitle, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                    boolean result = ChineseChessBusiness.updateGameOverReward(userDataMap);
                    if(!result){
                        getLogger().error("象棋大逃杀结束活动对局信息保存失败");
                    }
                }
            }

        }
    }

    /**
     * 判断这个位置有没有越界
     * @return
     */
    public static boolean canMoveNoChess(ChineseChessData chess, ChineseChessPosition pos){
        return chess.getPosition().getX() + pos.getX() >= 0 && chess.getPosition().getX() + pos.getX() < x &&
                chess.getPosition().getY() + pos.getY() >= 0 && chess.getPosition().getY() + pos.getY() < y;
    }

    public static int getX(){
        return x;
    }

    public static int getY(){
        return y;
    }
    
    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload chinese chess activity start");

        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ChineseChess.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("no chinese chess activity in show time");
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);
        Map<Integer, ChineseChessConfig> tempChessMap = ChineseChessBusiness.getChessMap(tmpActivityInfo.getActivityId());
        Map<Integer, ChineseChessCreateConfig> tempChessCreateMap = ChineseChessBusiness.getChessCreateMap(tmpActivityInfo.getActivityId());
        Map<Integer, ChineseChessWeightConfig> tempChessWeightMap = ChineseChessBusiness.getChessWeightMap(tmpActivityInfo.getActivityId());
        Map<Integer, ChineseChessMoveConfig> tempConfigChessMoveMap = ChineseChessBusiness.getChessMoveMap(tmpActivityInfo.getActivityId());
        // 活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        if(activityConfigMap != null){
            if(activityConfigMap.containsKey("CHINESE_CHESS_SCORE_ITEM_ID")){
                CHINESE_CHESS_SCORE_ITEM_ID = activityConfigMap.get("CHINESE_CHESS_SCORE_ITEM_ID").getIntValue();
            }
            if(activityConfigMap.containsKey("CHINESE_CHESS_PLAY_ITEM_ID")){
                CHINESE_CHESS_PLAY_ITEM_ID = activityConfigMap.get("CHINESE_CHESS_PLAY_ITEM_ID").getIntValue();
            }
            if(activityConfigMap.containsKey("CHINESE_CHESS_POWER_ITEM_ID")){
                CHINESE_CHESS_POWER_ITEM_ID = activityConfigMap.get("CHINESE_CHESS_POWER_ITEM_ID").getIntValue();
            }
            if(activityConfigMap.containsKey("CHINESE_CHESS_REVIVE_ITEM_ID")){
                CHINESE_CHESS_REVIVE_ITEM_ID = activityConfigMap.get("CHINESE_CHESS_REVIVE_ITEM_ID").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_BOARD_XY")){
                BIGCHESS_BOARD_XY = activityConfigMap.get("BIGCHESS_BOARD_XY").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_FREE_PLAYNUM")){
                BIGCHESS_FREE_PLAYNUM = activityConfigMap.get("BIGCHESS_FREE_PLAYNUM").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_MYCHESS_SKIN_ADD")){
                BIGCHESS_MYCHESS_SKIN_ADD = activityConfigMap.get("BIGCHESS_MYCHESS_SKIN_ADD").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_EVALUATE_SCORE")){
                BIGCHESS_EVALUATE_SCORE = activityConfigMap.get("BIGCHESS_EVALUATE_SCORE").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_PLAYER_BEGIN_PLACE")){
                BIGCHESS_PLAYER_BEGIN_PLACE = activityConfigMap.get("BIGCHESS_PLAYER_BEGIN_PLACE").getValue();
            }

            if(activityConfigMap.containsKey("BIGCHESS_ENEMY_BEGIN_PLACE")){
                BIGCHESS_ENEMY_BEGIN_PLACE = activityConfigMap.get("BIGCHESS_ENEMY_BEGIN_PLACE").getValue();
            }

            if(activityConfigMap.containsKey("BIGCHESS_REBORN_COST")){
                BIGCHESS_REBORN_COST = activityConfigMap.get("BIGCHESS_REBORN_COST").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_NEW_PROTECT_REGION")){
                BIGCHESS_NEW_PROTECT_REGION = activityConfigMap.get("BIGCHESS_NEW_PROTECT_REGION").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_NEW_PROTECT_REBORN")){
                BIGCHESS_NEW_PROTECT_REBORN = activityConfigMap.get("BIGCHESS_NEW_PROTECT_REBORN").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_REBORN_ISOPEN")){
                BIGCHESS_REBORN_ISOPEN = activityConfigMap.get("BIGCHESS_REBORN_ISOPEN").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_EVALUATE_TIPS")){
                BIGCHESS_EVALUATE_TIPS = activityConfigMap.get("BIGCHESS_EVALUATE_TIPS").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_AICHESS_MAX_NUM_CHE")){
                BIGCHESS_AICHESS_MAX_NUM_CHE = activityConfigMap.get("BIGCHESS_AICHESS_MAX_NUM_CHE").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_AICHESS_MAX_NUM_MA")){
                BIGCHESS_AICHESS_MAX_NUM_MA = activityConfigMap.get("BIGCHESS_AICHESS_MAX_NUM_MA").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_AICHESS_BORNLIMIT_JIANG")){
                BIGCHESS_AICHESS_BORNLIMIT_JIANG = activityConfigMap.get("BIGCHESS_AICHESS_BORNLIMIT_JIANG").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_AICHESS_WITHJIANG_NUM")){
                BIGCHESS_AICHESS_WITHJIANG_NUM = activityConfigMap.get("BIGCHESS_AICHESS_WITHJIANG_NUM").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_SKILL_WAVE_NUM")){
                BIGCHESS_SKILL_WAVE_NUM = activityConfigMap.get("BIGCHESS_SKILL_WAVE_NUM").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_TRAIN_ISOPEN")){
                BIGCHESS_TRAIN_ISOPEN = activityConfigMap.get("BIGCHESS_TRAIN_ISOPEN").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_ROUND_TURN_SCORE")){
                BIGCHESS_ROUND_TURN_SCORE = activityConfigMap.get("BIGCHESS_ROUND_TURN_SCORE").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_CREATCHESS_T_PARAM")){
                BIGCHESS_CREATCHESS_T_PARAM = activityConfigMap.get("BIGCHESS_CREATCHESS_T_PARAM").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_CREATCHESS_O_PARAM")){
                BIGCHESS_CREATCHESS_O_PARAM = activityConfigMap.get("BIGCHESS_CREATCHESS_O_PARAM").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_CREATCHESS_O_VALUE")){
                BIGCHESS_CREATCHESS_O_VALUE = activityConfigMap.get("BIGCHESS_CREATCHESS_O_VALUE").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_CREATCHESS_PARAM")){
                BIGCHESS_CREATCHESS_PARAM = activityConfigMap.get("BIGCHESS_CREATCHESS_PARAM").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_WIN_ROUND")){
                BIGCHESS_WIN_ROUND = activityConfigMap.get("BIGCHESS_WIN_ROUND").getIntValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_WIN_REWARD")){
                BIGCHESS_WIN_REWARD = activityConfigMap.get("BIGCHESS_WIN_REWARD").getValue();
            }
            if(activityConfigMap.containsKey("BIGCHESS_EVALUATE_REWARD")){
                BIGCHESS_EVALUATE_REWARD = activityConfigMap.get("BIGCHESS_EVALUATE_REWARD").getValue();
            }

            debugUserIdList = StringUtils.stringToLongList(DEBUG_USERID_LIST,";");
            if(activityConfigMap.containsKey("DEBUG_USERID_LIST")){
                debugUserIdList = StringUtils.stringToLongList(activityConfigMap.get("DEBUG_USERID_LIST").getValue(),";");
            }
        }
        initConfig(tempChessMap,tempChessCreateMap,tempChessWeightMap,tempConfigChessMoveMap,tmpActivityInfo);
        reloadPlayerData();
        getLogger().info("reload chinese chess activity end");
    }

    private static void initConfig(Map<Integer, ChineseChessConfig> tempChessMap,Map<Integer, ChineseChessCreateConfig> tempChessCreateMap,Map<Integer, ChineseChessWeightConfig> tempChessWeightMap,Map<Integer, ChineseChessMoveConfig> tempConfigChessMoveMap, ActivityInfo tmpActivityInfo) {
        for(Integer chessId : tempChessMap.keySet()){
            ChineseChessConfig configData = tempChessMap.get(chessId);
            Map<Integer,Integer> skinIdAddMap = new ConcurrentHashMap<>();
            List<String> skinIdAddStrList = StringUtils.stringToStringList(configData.getChessSkinId(),"\\|");
            for(String skinIdAddStr : skinIdAddStrList){
                List<Integer> skinIdAddList = StringUtils.stringToIntegerList(skinIdAddStr,";");
                skinIdAddMap.put(skinIdAddList.get(0),skinIdAddList.get(1));
            }
            configData.setSkinIdList(skinIdAddMap);
            String canGoString = configData.getCanGo();
            List<String> moveList = StringUtils.stringToStringList(canGoString,"\\|");
            Map<Integer,ChineseChessPosition> positionMap = new ConcurrentHashMap<>();
            for(int i = 0;i < moveList.size(); i++){
                String moveStr = moveList.get(i);
                if("0".equals(moveStr)){
                    continue;
                }
                ChineseChessPosition position = new ChineseChessPosition();
                List<Integer> positionList = StringUtils.stringToIntegerList(moveStr,";");
                position.setX(positionList.get(0));
                position.setY(positionList.get(1));
                positionMap.put(i+1,position);
            }
            configData.setCanGoMap(positionMap);

            String moveLimitString = configData.getMoveLimit();
            List<String> limitList = StringUtils.stringToStringList(moveLimitString,"\\|");
            Map<Integer,ChineseChessPosition> limitMap = new ConcurrentHashMap<>();
            for(int i = 0;i < limitList.size(); i++){
                String limitStr = limitList.get(i);
                if("0".equals(limitStr)){
                    continue;
                }
                ChineseChessPosition position = new ChineseChessPosition();
                List<String> positionList = StringUtils.stringToStringList(limitStr,";");
                String limitIndexStr = positionList.get(2);
                List<Integer> indexList = StringUtils.stringToIntegerList(limitIndexStr,"-");
                for (Integer index : indexList){
                    StringUtils.stringToIntegerList(limitIndexStr,";");
                    position.setX(Integer.valueOf(positionList.get(0)));
                    position.setY(Integer.valueOf(positionList.get(1)));
                    limitMap.put(index,position);
                }
            }
            configData.setMoveLimitMap(limitMap);
        }
        //加上玩家自己的棋子
        ChineseChessConfig playerChessConfig = new ChineseChessConfig();
        playerChessConfig.setId(0);
        Map<Integer,Integer> playerSkinIdAddMap = new ConcurrentHashMap<>();
        List<String> playerSkinIdAddStrList = StringUtils.stringToStringList(BIGCHESS_MYCHESS_SKIN_ADD,"\\|");
        for(String skinIdAddStr : playerSkinIdAddStrList){
            List<Integer> skinIdAddList = StringUtils.stringToIntegerList(skinIdAddStr,";");
            playerSkinIdAddMap.put(skinIdAddList.get(0),skinIdAddList.get(1));
            playerChessConfig.setSkinIdList(playerSkinIdAddMap);
        }
        tempChessMap.put(0,playerChessConfig);
        for(ChineseChessWeightConfig weightConfig : tempChessWeightMap.values()){
            Map<Integer,Integer> weightMap = new ConcurrentHashMap<>();
            String weightStr = weightConfig.getWeight();
            List<Integer> weightList = StringUtils.stringToIntegerList(weightStr,";");
            weightMap.put(eChineseChessPiecesType.Pawns.getValue(),weightList.get(0));
            weightMap.put(eChineseChessPiecesType.Mandarins.getValue(),weightList.get(1));
            weightMap.put(eChineseChessPiecesType.Elephants.getValue(),weightList.get(2));
            weightMap.put(eChineseChessPiecesType.Cannons.getValue(),weightList.get(3));
            weightMap.put(eChineseChessPiecesType.Knights.getValue(),weightList.get(4));
            weightMap.put(eChineseChessPiecesType.Rooks.getValue(),weightList.get(5));
            weightConfig.setWeightMap(weightMap);
        }
        int tempRecoverHp;
        int tempX;
        int tempY;

        List<Integer> tempReviveList;
        List<ChineseChessData> tempInitialEnemyChessList = new ArrayList<>();

        List<Integer> tempScoreShuaiList;
        List<ChineseChessData> tempShuaiAttendList = new ArrayList<>();

        List<Integer> tempIntegerList;
        List<String> tempStringList;
        tempRecoverHp = BIGCHESS_FREE_PLAYNUM;

        tempIntegerList = StringUtils.stringToIntegerList(BIGCHESS_BOARD_XY,"\\*");
        tempX = tempIntegerList.get(0);
        tempY = tempIntegerList.get(1);

        tempIntegerList = StringUtils.stringToIntegerList(BIGCHESS_PLAYER_BEGIN_PLACE,";");
        ChineseChessPosition tempInitialPosition = new ChineseChessPosition();
        tempInitialPosition.setX(tempIntegerList.get(0));
        tempInitialPosition.setY(tempIntegerList.get(1));

        tempStringList = StringUtils.stringToStringList(BIGCHESS_ENEMY_BEGIN_PLACE,"\\|");
        for(int i = 0;i < tempStringList.size();i++){
            String enemyStr = tempStringList.get(i);
            tempIntegerList = StringUtils.stringToIntegerList(enemyStr,";");
            ChineseChessData tempChess = new ChineseChessData();
            ChineseChessPosition tempPosition = new ChineseChessPosition();
            tempPosition.setX(tempIntegerList.get(0));
            tempPosition.setY(tempIntegerList.get(1));
            tempChess.setChessId(tempIntegerList.get(2));
            tempChess.setChessNo(i + 1);
            tempChess.setPosition(tempPosition);
            tempInitialEnemyChessList.add(tempChess);
        }

        tempReviveList = StringUtils.stringToIntegerList(BIGCHESS_REBORN_COST,"\\|");

        tempScoreShuaiList = StringUtils.stringToIntegerList(BIGCHESS_AICHESS_BORNLIMIT_JIANG,";");

        tParamList = StringUtils.stringToIntegerList(BIGCHESS_CREATCHESS_T_PARAM,"\\|");
        oParamList = StringUtils.stringToIntegerList(BIGCHESS_CREATCHESS_O_PARAM,"\\|");
        oValueList = StringUtils.stringToIntegerList(BIGCHESS_CREATCHESS_O_VALUE,"\\|");
        paramList = StringUtils.stringToIntegerList(BIGCHESS_CREATCHESS_PARAM,"\\|");

        Map<Integer, String> tempGameOverRewardMap = new ConcurrentHashMap<>();
        List<String> gameOverScoreList = StringUtils.stringToStringList(BIGCHESS_EVALUATE_SCORE,"\\|");
        List<String> gameOverRewardList = StringUtils.stringToStringList(BIGCHESS_EVALUATE_REWARD,"\\|");
        for(int i = 0 ; i < gameOverScoreList.size() ; i++){
            String idScoreString = gameOverScoreList.get(i);
            List<Integer> intList = StringUtils.stringToIntegerList(idScoreString,";");
            if(i+1 < gameOverScoreList.size()){
                List<Integer> nextIntList = StringUtils.stringToIntegerList(gameOverScoreList.get(i+1),";");
                tempGameOverRewardMap.put(nextIntList.get(1),gameOverRewardList.get(i));
            }else{
                tempGameOverRewardMap.put(Integer.MAX_VALUE,gameOverRewardList.get(i));
            }
        }

        tempIntegerList = StringUtils.stringToIntegerList(BIGCHESS_AICHESS_WITHJIANG_NUM,";");
        for(int i = 0;i < tempIntegerList.get(1);i++){
            ChineseChessData tempChess = new ChineseChessData();
            tempChess.setChessId(tempIntegerList.get(0));
            tempShuaiAttendList.add(tempChess);
        }



        recoverHp = tempRecoverHp;
        initialSelfPosition = tempInitialPosition;
        initialEnemyChessList = tempInitialEnemyChessList;
        scoreShuaiList = tempScoreShuaiList;
        shuaiAttendList = tempShuaiAttendList;
        gameOverRewardMap = tempGameOverRewardMap;

        x = tempX;
        y = tempY;
        initPositionMap(tempX,tempY);
        reviveList = tempReviveList;
        configChessMap = tempChessMap;
        configChessCreateMap = tempChessCreateMap;
        configChessWeightMap = tempChessWeightMap;
        configChessMoveMap = tempConfigChessMoveMap;
        activityInfo = tmpActivityInfo;
    }


    private static void initPositionMap(int x,int y){
        Map<Integer,Map<Integer,ChineseChessPosition>> map = new ConcurrentHashMap<>();
        Map<String,ChineseChessPosition> simpleMap = new ConcurrentHashMap<>();
        for(int i = 0 ; i < x;i++){
            if(!map.containsKey(i)){
                map.put(i,new ConcurrentHashMap<>());
            }
            for(int j = 0 ; j < y;j++){
                map.get(i).put(j,new ChineseChessPosition(i,j));
                simpleMap.put(i+"-"+j,new ChineseChessPosition(i,j));
            }
        }
        allPositionMap = map;
        allPositionSimpleMap = simpleMap;
    }

    private static void reloadPlayerData() {
        if (ActivityMgr.activityInShowTime(activityInfo)) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.ChineseChess.getValue())) {
                    ChineseChessModule module = player.getModule(ChineseChessModule.class);
                    module.initUserData(activityInfo.getActivityId());
                    module.syncConfigData();
                    module.crossSyncUserData();
                    ChineseChessPracticeModule modulePractice = player.getModule(ChineseChessPracticeModule.class);
                    modulePractice.initPracticeUserData(activityInfo.getActivityId());
                }
            }
        }
    }

    /**
     * 是否在清空道具时间内
     */
    public static boolean isActivityInClearTime() {
        return ActivityMgr.activityInRewardPeriod(activityInfo);
    }

    /**
     * 获取展示中的活动ID（0表示没活动）
     */
    public static int getShowTimeActivityId() {
        return activityInfo == null ? 0 : activityInfo.getActivityId();
    }

    public static boolean activityInTime(){
        return ActivityMgr.activityInTime(activityInfo);
    }

    public static boolean activityInShowTime(){
        return ActivityMgr.activityInShowTime(activityInfo);
    }

    public static boolean activityInRewardPeriod(){
        return ActivityMgr.activityInRewardPeriod(activityInfo);
    }



    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

}
